|
| 1 | +import type { |
| 2 | + APIResponse, |
| 3 | + BasicRequestOptions, |
| 4 | + ApiClientConfig, |
| 5 | + RequestInterceptor, |
| 6 | + ResponseInterceptor, |
| 7 | + RequestInitWithMethod, |
| 8 | + RequestOptions, |
| 9 | +} from './types.js'; |
| 10 | + |
| 11 | +export class APIClient { |
| 12 | + public interceptors: {request: RequestInterceptor[]; response: ResponseInterceptor[]} = { |
| 13 | + request: [], |
| 14 | + response: [], |
| 15 | + }; |
| 16 | + |
| 17 | + constructor( |
| 18 | + private baseUrl: string, |
| 19 | + private config?: ApiClientConfig |
| 20 | + ) {} |
| 21 | + |
| 22 | + setBaseURL(baseUrl: string): void { |
| 23 | + this.baseUrl = baseUrl; |
| 24 | + } |
| 25 | + |
| 26 | + setConfig(config: Partial<RequestInit>): void { |
| 27 | + this.config = config; |
| 28 | + } |
| 29 | + |
| 30 | + private formatData(response: Response, options?: BasicRequestOptions): Promise<any> { |
| 31 | + const responseType = options?.responseType || 'json'; |
| 32 | + switch (responseType) { |
| 33 | + case 'arraybuffer': |
| 34 | + return response.arrayBuffer(); |
| 35 | + case 'blob': |
| 36 | + return response.blob(); |
| 37 | + case 'text': |
| 38 | + return response.text(); |
| 39 | + case 'json': |
| 40 | + default: |
| 41 | + return response.json(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + async request(endpoint: string, options: RequestOptions): Promise<Response> { |
| 46 | + const url = new URL(endpoint, this.baseUrl); |
| 47 | + |
| 48 | + if (options.params) { |
| 49 | + for (const [key, param] of Object.entries(options.params)) { |
| 50 | + if (param !== null && param !== undefined) { |
| 51 | + url.searchParams.append(key, String(param)); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + let requestOptions: RequestInitWithMethod = { |
| 57 | + method: options.method.toUpperCase(), |
| 58 | + ...this.config, |
| 59 | + }; |
| 60 | + |
| 61 | + if (options.headers) { |
| 62 | + requestOptions.headers = { |
| 63 | + ...requestOptions.headers, |
| 64 | + ...options.headers, |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + if (this.config?.auth) { |
| 69 | + const {username, password} = this.config.auth; |
| 70 | + const encoded = btoa(`${username}:${password}`); |
| 71 | + |
| 72 | + requestOptions.headers = { |
| 73 | + ...requestOptions.headers, |
| 74 | + Authorization: `Basic ${encoded}`, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + if (options.data) { |
| 79 | + if (options.data instanceof Object) { |
| 80 | + requestOptions.headers = { |
| 81 | + ...requestOptions.headers, |
| 82 | + 'Content-Type': 'application/json', |
| 83 | + }; |
| 84 | + options.data = JSON.stringify(options.data); |
| 85 | + } |
| 86 | + requestOptions.body = options.data; |
| 87 | + } |
| 88 | + |
| 89 | + if (this.interceptors.request.length > 0) { |
| 90 | + for (const interceptor of this.interceptors.request) { |
| 91 | + requestOptions = await interceptor(url, requestOptions); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + const response = await fetch(url, requestOptions); |
| 96 | + if (!response.ok) { |
| 97 | + throw new Error(`${options.method.toUpperCase()} request failed: ${response.statusText}`); |
| 98 | + } |
| 99 | + |
| 100 | + if (this.interceptors.response.length > 0) { |
| 101 | + for (const interceptor of this.interceptors.response) { |
| 102 | + await interceptor(response); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return response; |
| 107 | + } |
| 108 | + |
| 109 | + async delete<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 110 | + const request = await this.request(endpoint, {...options, method: 'DELETE'}); |
| 111 | + const requestData = await this.formatData(request, options); |
| 112 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 113 | + } |
| 114 | + |
| 115 | + async get<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 116 | + const request = await this.request(endpoint, {...options, method: 'GET'}); |
| 117 | + const requestData = await this.formatData(request, options); |
| 118 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 119 | + } |
| 120 | + |
| 121 | + async head<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 122 | + const request = await this.request(endpoint, {...options, method: 'HEAD'}); |
| 123 | + const requestData = await this.formatData(request, options); |
| 124 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 125 | + } |
| 126 | + |
| 127 | + async patch<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 128 | + const request = await this.request(endpoint, {...options, data, method: 'PATCH'}); |
| 129 | + const requestData = await this.formatData(request, options); |
| 130 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 131 | + } |
| 132 | + |
| 133 | + async options<T = any>(endpoint: string, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 134 | + const request = await this.request(endpoint, {...options, method: 'OPTIONS'}); |
| 135 | + const requestData = await this.formatData(request, options); |
| 136 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 137 | + } |
| 138 | + |
| 139 | + async post<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 140 | + const request = await this.request(endpoint, {data, ...options, method: 'POST'}); |
| 141 | + const requestData = await this.formatData(request, options); |
| 142 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 143 | + } |
| 144 | + |
| 145 | + async put<T = any>(endpoint: string, data?: any, options?: BasicRequestOptions): Promise<APIResponse<T>> { |
| 146 | + const request = await this.request(endpoint, {data, ...options, method: 'PUT'}); |
| 147 | + const requestData = await this.formatData(request, options); |
| 148 | + return {data: requestData, headers: request.headers, status: request.status}; |
| 149 | + } |
| 150 | +} |
0 commit comments