import merge from 'lodash.merge'; import { Http } from './http'; import { Resource } from './resource'; import { BodyData, IApi } from './types'; import { toJSON } from './utils'; const defaultOptions = { headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, }; export class Api extends Http implements IApi { public get(path: string, options?: RequestInit) { return super.get(path, options).then((res) => toJSON(res)); } public post( path: string, data?: Req, options?: RequestInit, ) { return super.post(path, data, options).then((res) => toJSON(res)); } public put( path: string, data?: Req, options?: RequestInit, ) { return super.put(path, data, options).then((res) => toJSON(res)); } public patch( path: string, data?: Req, options?: RequestInit, ) { return super .patch(path, data, options) .then((res) => toJSON(res)); } public delete(path: string, options?: RequestInit) { return super.delete(path, options).then((res) => toJSON(res)); } public search(path: string, query: object, options?: RequestInit) { return super.search(path, query, options).then((res) => toJSON(res)); } public resource(endpoint: string) { return new Resource(this, endpoint); } public static create(url?: string, options?: RequestInit) { return new Api(url ?? '', merge({}, defaultOptions, options)); } }