/** * A basic api request service * * It will send basic http requests and returns raw results */ import type {AssociativeArray} from "~/types/data"; import {HTTP_METHOD} from "~/types/enum/data"; import type {FetchOptions} from "ofetch"; import type {$Fetch} from "nitropack"; class ApiRequestService { private readonly fetch: $Fetch public constructor( fetch: $Fetch ) { this.fetch = fetch } /** * Send a GET request * * @param url * @param query */ public async get( url: string, query: AssociativeArray | null = null ) { return await this.request(HTTP_METHOD.GET, url, null, query) } /** * Send a POST request * * @param url * @param body * @param query */ public async post( url: string, body: string | null = null, query: AssociativeArray | null = null ) { return await this.request(HTTP_METHOD.POST, url, body, query) } /** * Send a PUT request * * @param url * @param body * @param query */ public async put( url: string, body: string | null = null, query: AssociativeArray | null = null ) { return await this.request(HTTP_METHOD.PUT, url, body, query) } /** * Send a DELETE request * * @param url * @param query */ public async delete( url: string, query: AssociativeArray | null = null ) { return await this.request(HTTP_METHOD.DELETE, url, null, query) } /** * Send an http request * * @param method * @param url * @param body * @param query * @protected */ protected async request( method: HTTP_METHOD, url: string, body: string | null = null, query: AssociativeArray | null = null ): Promise { const config: FetchOptions = { method } if (query) { config.query = query } if (method === HTTP_METHOD.POST || method === HTTP_METHOD.PUT) { config.body = body } // @ts-ignore return this.fetch(url, config) } } export default ApiRequestService