sophie-http
TypeScript icon, indicating that this package has built-in type declarations

1.0.8-beta • Public • Published

Sophie HTTP

Provides simple components for creating http requests. Ideal for an API. It uses Promises.

Part of the framework Sophie

npm version npm dependencies npm downloads

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

Changelog

Contributing contributions welcome

License

This software is licensed under the terms of the MIT license. See LICENSE for the full license.

Package Sidebar

Install

npm i sophie-http

Weekly Downloads

1

Version

1.0.8-beta

License

MIT

Unpacked Size

154 kB

Total Files

68

Last publish

Collaborators

  • xtrimsystems