import type { AxiosInstance, AxiosResponse } from 'axios'; import type { z } from 'zod'; import type { ListSchema } from '../types'; import type { AsyncListResult, AsyncResult } from '../utilities'; import { errorResult, successListResult, successResult, UnsupportedError, } from '../utilities'; import type { ResourceQuery } from './BaseResourceRepository'; import { BaseResourceRepository } from './BaseResourceRepository'; export class ResourceRepository< GetResponseType, ListResponseType extends z.infer, CreateInputType, UpdateInputType, PatchInputType > extends BaseResourceRepository { protected readonly _schemas: { get?: z.ZodType; list?: z.ZodType; createInput?: z.ZodType; updateInput?: z.ZodType; patchInput?: z.ZodType; }; constructor( axios: AxiosInstance, baseUrl: string, schemas: { get?: z.ZodType; list?: z.ZodType; createInput?: z.ZodType; updateInput?: z.ZodType; patchInput?: z.ZodType; } ) { super(axios, baseUrl); this._schemas = schemas; } public async get( id: string, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get) throw new UnsupportedError(); let data: GetResponseType; try { const request = await this._axios.get( `${this._baseUrl}/${id}`, this.getRequestConfig(query) ); data = this._schemas.get.parse(request.data); } catch (error) { return errorResult(error as Error); } return successResult(data); } public async list(query?: ResourceQuery): AsyncListResult { return this.getListPage(query); } public async getListPage( query: ResourceQuery = {}, url: string = this._baseUrl ): AsyncListResult { if (!this._schemas.list) throw new UnsupportedError(); let request: AxiosResponse; let data: ListResponseType; try { request = await this._axios.get(url, this.getRequestConfig(query)); data = this._schemas.list.parse(request.data); } catch (error) { return errorResult(error as Error); } const hasPreviousPage = !!data._links.previous?.href; const hasNextPage = !!data._links.next?.href; const previousFn = () => this.getListPage({}, data._links.previous?.href); const nextFn = () => this.getListPage({}, data._links.next?.href); const previous = hasPreviousPage ? previousFn : undefined; const next = hasNextPage ? nextFn : undefined; return successListResult(data, previous, next); } public async create( inputData: CreateInputType, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get || !this._schemas.createInput) throw new UnsupportedError(); let responseData: GetResponseType; try { const validatedInput = this._schemas.createInput.parse(inputData); const request = await this._axios.post( this._baseUrl, validatedInput, this.getRequestConfig(query) ); responseData = this._schemas.get.parse(request.data); } catch (error) { return errorResult(error as Error); } return successResult(responseData); } public async update( id: string, inputData: UpdateInputType, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get || !this._schemas.updateInput) throw new UnsupportedError(); let responseData: GetResponseType; try { const validatedInput = this._schemas.updateInput.parse(inputData); const request = await this._axios.put( `${this._baseUrl}/${id}`, validatedInput, this.getRequestConfig(query) ); responseData = this._schemas.get.parse(request.data); } catch (error) { return errorResult(error as Error); } return successResult(responseData); } public async patch( id: string, inputData: PatchInputType, query?: ResourceQuery ): AsyncResult { if (!this._schemas.get || !this._schemas.patchInput) throw new UnsupportedError(); let responseData: GetResponseType; try { const validatedInput = this._schemas.patchInput.parse(inputData); const request = await this._axios.patch( `${this._baseUrl}/${id}`, validatedInput, this.getRequestConfig(query) ); responseData = this._schemas.get.parse(request.data); } catch (error) { return errorResult(error as Error); } return successResult(responseData); } public async delete(id: string): AsyncResult { try { await this._axios.delete(`${this._baseUrl}/${id}`); } catch (error) { return errorResult(error as Error); } return successResult(null); } }