1.0.8-beta • Published 6 years ago
sophie-http v1.0.8-beta
Sophie HTTP
Provides simple components for creating http requests. Ideal for an API. It uses Promises.
Part of the framework Sophie
INSTALLATION
yarn add sophie-http
USAGE
QUERY
To make a GET request use the Query object
// Import objects
import {
Query,
HtmlRequestContentType,
XhrResponse
} from "sophie-http";
// Instantiate Query
const query: Query = new Query();
// Set the API endpoint
query.setUrl('api/posts');
// Make the request, returns a Promise
query.getData(new HtmlRequestContentType())
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
COMMAND
To make a POST, PUT or DELETE request use the Command object
// Import objects
import {
Command,
JsonRequestContentType,
XhrResponse
} from "sophie-http";
// Instantiate Command
const command: Command = new Command();
// Set the API endpoint
command.setUrl('api/posts');
command.saveData(new JsonRequestContentType(), { title: "New post", text: "Lorem Ipsum... " })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
command.updateData(new JsonRequestContentType(), { id: 1, title: "Updated title" })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
command.deleteData(new JsonRequestContentType(), { id: 1 })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
Extending Query or Command
For example, to pre-set the url
// Import objects
import {
Query,
Command,
JsonRequestContentType,
XhrResponse
} from "sophie-http";
const URI_API_POSTS = 'api/posts';
export class PostsQuery extends Query
{
public constructor ()
{
super();
this.url = URI_API_POSTS;
}
}
export class PostsCommand extends Command
{
public constructor ()
{
super();
this.url = URI_API_POSTS;
}
}
const postsQuery: Query = new PostsQuery();
const postsCommand: Command = new PostsCommand();
postsQuery.getData(new JsonRequestContentType())
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
postsCommand.saveData(new JsonRequestContentType(), { title: "New post", text: "Lorem Ipsum... " })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
postsCommand.updateData(new JsonRequestContentType(), { id: 1, title: "Updated title" })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
postsCommand.deleteData(new JsonRequestContentType(), { id: 1 })
.then((res: XhrResponse) => {
console.log(res);
})
.catch((res: XhrResponse) => {
console.log(res);
});
Changelog
Contributing
License
This software is licensed under the terms of the MIT license. See LICENSE for the full license.
1.0.8-beta
6 years ago
1.0.7-beta
6 years ago
1.0.6-beta
6 years ago
1.0.5-beta
6 years ago
1.0.4-beta
6 years ago
1.0.2-beta
6 years ago
1.0.1-beta
6 years ago
1.0.0-beta
6 years ago