@d1g1t/type-api
TypeScript icon, indicating that this package has built-in type declarations

2.1.0 • Public • Published

Typescript enabled API library

Greenkeeper badge

Build Status NPM version Standard Version styled with prettier Conventional Commits

Consumption of published library:

yarn add type-api or npm install type-api

Webpack

import {RestApi} from 'type-api'

/**
 * Define rest api
 */
class PostApi extends RestApi {
    baseUrl = 'https://jsonplaceholder.typicode.com'
    endpoint = '/posts'
}

/**
 * Initialize api
 */
export const postApi = new PostApi()

// Get one by ID
try {
    const response = postApi.findById(1)

    console.log(response)
} catch (error) {
    console.error(error)
}

// Create entity
try {
    const postData =   {
        "userId": 1,
        "title": "New user",
        "body": "Some content"
    },
    const response = postApi.create(postData)

    console.log(response)
} catch (error) {
    console.error(error)
}

// Update entity
try {
    const postData =   {
        "userId": 1,
        "title": "New user",
        "body": "Some content"
    },
    const response = postApi.update(1, postData)

    console.log(response)
} catch (error) {
    console.error(error)
}

Available methods

Rest API

  • findById(id) : GET - retrieve one record as object
  • findAll() : GET - retrieve all records as list
  • findOne({name: 'some name'}) : GET - retrieve one record from list of response results
  • find({limit: 3}) : GET - retrieve records as list and generate query string from object
  • create({name: 'Some Name'}) : POST - submit object for creation
  • update(1, {name: 'Some Name'}) : PUT - submit object for update

Base API

  • get('custom')
  • post('custom', postData)
  • put('custom', putData)
  • patch('custom', patchData)

Readme

Keywords

none

Package Sidebar

Install

npm i @d1g1t/type-api

Weekly Downloads

1

Version

2.1.0

License

MIT

Unpacked Size

2.9 MB

Total Files

57

Last publish

Collaborators

  • azmenak