Files
insomnia/packages/insomnia-api/src/fetch.ts
Curry Yang 0bbae307fd refactor: migrate projects request to insomnia-api package - [INS-1874] (#9574)
* refactor: migrate projects request to insomnia-api package

* fix: cr

* refactor: error handling

* fix: add isApiError

* fix: error

* fix
2026-01-21 09:14:53 +00:00

39 lines
1.1 KiB
TypeScript

export interface FetchConfig {
method: 'POST' | 'PUT' | 'GET' | 'DELETE' | 'PATCH';
path: string;
sessionId: string | null;
organizationId?: string | null;
data?: unknown;
origin?: string;
headers?: Record<string, string>;
timeout?: number;
}
export type Fetch = <T = void>(options: FetchConfig) => Promise<T>;
export let fetch: Fetch = <T = void>(_options: FetchConfig): Promise<T> => {
throw new Error('Fetch has not been configured. Please call configureFetch() at application startup.');
};
let configured = false;
export function configureFetch(_fetch: Fetch) {
if (configured) {
throw new Error('Fetch has already been configured and cannot be re-configured.');
}
fetch = _fetch;
configured = true;
}
export class ResponseFailError extends Error {
response: Response;
constructor(name: string, msg: string, response: Response) {
super(msg);
this.name = name || 'ResponseFailError';
this.response = response;
}
}
export const isApiError = (error: unknown): error is ResponseFailError => {
return error instanceof ResponseFailError;
};