Skip to content
This repository has been archived by the owner on May 22, 2023. It is now read-only.

Commit

Permalink
Improve minification build (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow authored May 1, 2023
1 parent ff3174a commit 22197a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/honest-mayflies-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openapi-fetch': patch
---

Add missing type defs for minified build
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"build": "npm run build:clean && npm run build:ts && npm run build:ts-min",
"build:clean": "del dist",
"build:ts": "tsc -p tsconfig.build.json",
"build:ts-min": "esbuild --bundle src/index.ts --format=esm --minify --outfile=dist/index.min.js",
"build:ts-min": "esbuild --bundle src/index.ts --format=esm --minify --outfile=dist/index.min.js && cp dist/index.d.ts dist/index.min.d.ts",
"lint": "npm run lint:js",
"lint:js": "npx prettier --check \"{src,test}/**/*\"",
"test": "npm run test:ts && npm run test:js",
Expand Down
22 changes: 6 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ export type RequestBodyJSON<OperationObj> = FilterKeys<RequestBodyContent<Operat
export type RequestBody<OperationObj> = undefined extends RequestBodyJSON<OperationObj> ? { body?: RequestBodyJSON<OperationObj> } : { body: RequestBodyJSON<OperationObj> };
export type QuerySerializer<OperationObj> = { querySerializer?: (query: OperationObj extends { parameters: { query: any } } ? OperationObj['parameters']['query'] : Record<string, unknown>) => string };
export type FetchOptions<OperationObj> = Params<OperationObj> & RequestBody<OperationObj> & Omit<RequestInit, 'body'> & QuerySerializer<OperationObj>;
export type TruncatedResponse = Omit<Response, 'arrayBuffer' | 'blob' | 'body' | 'clone' | 'formData' | 'json' | 'text'>;
export type Success<OperationObj> = FilterKeys<FilterKeys<OperationObj, OkStatus>, 'content'>;
export type Error<OperationObj> = FilterKeys<FilterKeys<OperationObj, ErrorStatus>, 'content'>;
export type FetchResponse<T> =
| { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T['responses']>, JSONLike>> : unknown; error?: never; response: TruncatedResponse }
| { data?: never; error: T extends { responses: any } ? NonNullable<FilterKeys<Error<T['responses']>, JSONLike>> : unknown; response: TruncatedResponse };
| { data: T extends { responses: any } ? NonNullable<FilterKeys<Success<T['responses']>, JSONLike>> : unknown; error?: never; response: Response }
| { data?: never; error: T extends { responses: any } ? NonNullable<FilterKeys<Error<T['responses']>, JSONLike>> : unknown; response: Response };

export default function createClient<Paths extends {}>(options?: ClientOptions) {
const defaultHeaders = new Headers({
Expand Down Expand Up @@ -71,26 +70,17 @@ export default function createClient<Paths extends {}>(options?: ClientOptions)
}

// fetch!
const res = await fetch(finalURL, {
const response = await fetch(finalURL, {
redirect: 'follow',
...options,
...init,
headers: baseHeaders,
body: typeof requestBody === 'string' ? requestBody : JSON.stringify(requestBody),
});
const response: TruncatedResponse = {
bodyUsed: res.bodyUsed,
headers: res.headers,
ok: res.ok,
redirected: res.redirected,
status: res.status as any,
statusText: res.statusText,
type: res.type,
url: res.url,
};

// don’t parse JSON if status is 204, or Content-Length is '0'
const body = res.status === 204 || res.headers.get('Content-Length') === '0' ? {} : await res.json();
return res.ok ? { data: body, response } : { error: body, response: response };
const body = response.status === 204 || response.headers.get('Content-Length') === '0' ? {} : await response.json();
return response.ok ? { data: body, response } : { error: body, response: response };
}

return {
Expand Down

0 comments on commit 22197a1

Please sign in to comment.