Frontend-Common/api
grigoriishveps f13178df39
Delete tranform object to string with JSON.stringify (#6)
2021-11-02 11:57:00 +03:00
..
ApiMethodFactory.ts Delete tranform object to string with JSON.stringify (#6) 2021-11-02 11:57:00 +03:00
HttpMethod.ts Add modules (#3) 2021-08-05 12:50:37 +03:00
README.md Add modules (#3) 2021-08-05 12:50:37 +03:00
error.ts Add modules (#3) 2021-08-05 12:50:37 +03:00
index.ts Add modules (#3) 2021-08-05 12:50:37 +03:00
request.ts Add modules (#3) 2021-08-05 12:50:37 +03:00

README.md

api

This module helps define callable API methods.

Usage example

import { ApiMethodFactory, HttpMethod } from 'lib/api'

const { make } = new ApiMethodFactory({ apiPrefix: '/api' })

interface User {
  id: string
  name: string
  email: string
}

interface UserRequest {
  id: string
}

interface UserListRequest {
  limit: number
  offset: number
}

type UserResponse = User

type UserListResponse = User[]

type _ = unknown

const API = {
  user: {
    get: make<UserResponse, UserRequest>('/users/%(id)s', HttpMethod.GET, {
      path: ['id'],
    }),
    list: make<UserListResponse, UserListRequest>('/users', HttpMethod.GET, {
      query: ['limit', 'offset'],
    }),
    delete: make<_, UserRequest>('/users/%(id)s', HttpMethod.DELETE, {
      path: ['id'],
    }),
  },
}

const run = async () => {
  const users = await API.user.list({ limit: 10, offset: 0 })

  users.forEach(async (user) => {
    await API.user.delete({
      id: user.id,
    })
  })
}