-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: Use Proxy API to reduce bundle size
feat!: Use Proxy API to reduce bundle size chore add encoding fix jest errors fix some tests fix several broken tests fix obsolete config test allow setting header and signal in each request add wait for util prettify directory fix broken tests add meta options fix paginator
- Loading branch information
Showing
130 changed files
with
2,343 additions
and
3,312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { snakeCase } from 'change-case'; | ||
|
||
import type { Http, HttpMetaParams } from '../http'; | ||
import { Paginator } from '../paginator'; | ||
|
||
function noop(): void { | ||
// | ||
} | ||
|
||
const get = | ||
<T>(http: Http, context: string[]) => | ||
(_: unknown, key: string) => { | ||
// Promise not permitted | ||
if (key === 'then') { | ||
return; | ||
} | ||
|
||
return createBuilder<T>(http, [...context, key]); | ||
}; | ||
|
||
const apply = | ||
(http: Http, context: string[]) => | ||
(_1: unknown, _2: unknown, args: unknown[]): unknown => { | ||
const action = context.pop(); | ||
|
||
if (action == undefined) { | ||
throw new Error('No action specified'); | ||
} | ||
|
||
if (action === 'select') { | ||
return createBuilder(http, [...context, ...(args as string[])]); | ||
} | ||
|
||
const data = args[0]; | ||
const meta = args[1] as HttpMetaParams; | ||
const path = '/' + context.map((name) => snakeCase(name)).join('/'); | ||
|
||
switch (action) { | ||
case 'fetch': { | ||
return http.get(path, data, meta); | ||
} | ||
case 'create': { | ||
return http.post(path, data, meta); | ||
} | ||
case 'update': { | ||
return http.patch(path, data, meta); | ||
} | ||
case 'remove': { | ||
return http.delete(path, data, meta); | ||
} | ||
case 'list': { | ||
return new Paginator(http, path, data); | ||
} | ||
default: { | ||
const customAction = [path, snakeCase(action)].join('/'); | ||
return http.post(customAction, data); | ||
} | ||
} | ||
}; | ||
|
||
export const createBuilder = <T>(http: Http, context: string[] = []): T => { | ||
return new Proxy(noop, { | ||
get: get(http, context), | ||
apply: apply(http, context), | ||
}) as T; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,76 @@ | ||
import type { RequestInit } from '@mastojs/ponyfills'; | ||
|
||
import type { Http, HttpRequestParams, HttpRequestResult } from './http'; | ||
import type { Encoding } from '../serializers'; | ||
import type { | ||
Http, | ||
HttpMetaParams, | ||
HttpRequestParams, | ||
HttpRequestResult, | ||
} from './http'; | ||
|
||
export abstract class BaseHttp implements Http { | ||
abstract request(params: HttpRequestParams): Promise<HttpRequestResult>; | ||
|
||
get<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> { | ||
get<T>( | ||
path: string, | ||
data?: unknown, | ||
meta: HttpMetaParams<Encoding> = {}, | ||
): Promise<T> { | ||
return this.request({ | ||
method: 'GET', | ||
path, | ||
searchParams: data as Record<string, unknown>, | ||
requestInit: { | ||
method: 'GET', | ||
...init, | ||
}, | ||
...meta, | ||
}).then((response) => response.data as T); | ||
} | ||
|
||
post<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> { | ||
post<T>( | ||
path: string, | ||
data?: unknown, | ||
meta: HttpMetaParams<Encoding> = {}, | ||
): Promise<T> { | ||
return this.request({ | ||
method: 'POST', | ||
path, | ||
body: data as Record<string, unknown>, | ||
requestInit: { | ||
method: 'POST', | ||
...init, | ||
}, | ||
...meta, | ||
}).then((response) => response.data as T); | ||
} | ||
|
||
delete<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> { | ||
delete<T>( | ||
path: string, | ||
data?: unknown, | ||
meta: HttpMetaParams<Encoding> = {}, | ||
): Promise<T> { | ||
return this.request({ | ||
method: 'DELETE', | ||
path, | ||
body: data as Record<string, unknown>, | ||
requestInit: { | ||
method: 'DELETE', | ||
...init, | ||
}, | ||
...meta, | ||
}).then((response) => response.data as T); | ||
} | ||
|
||
put<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> { | ||
put<T>( | ||
path: string, | ||
data?: unknown, | ||
meta: HttpMetaParams<Encoding> = {}, | ||
): Promise<T> { | ||
return this.request({ | ||
method: 'PUT', | ||
path, | ||
body: data as Record<string, unknown>, | ||
requestInit: { | ||
method: 'PUT', | ||
...init, | ||
}, | ||
...meta, | ||
}).then((response) => response.data as T); | ||
} | ||
|
||
patch<T>(path: string, data?: unknown, init: RequestInit = {}): Promise<T> { | ||
patch<T>( | ||
path: string, | ||
data?: unknown, | ||
meta: HttpMetaParams<Encoding> = {}, | ||
): Promise<T> { | ||
return this.request({ | ||
method: 'PATCH', | ||
path, | ||
body: data as Record<string, unknown>, | ||
requestInit: { | ||
method: 'PATCH', | ||
...init, | ||
}, | ||
...meta, | ||
}).then((response) => response.data as T); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Headers } from '@mastojs/ponyfills'; | ||
|
||
import { getEncoding } from './get-encoding'; | ||
|
||
test.each([ | ||
[{ 'Content-Type': 'application/json; charset=utf-8' }, 'json'], | ||
[{ 'content-type': 'application/json; charset=utf-8' }, 'json'], | ||
[{ 'Content-Type': 'application/json' }, 'json'], | ||
[{}, undefined], | ||
])('removes charset from content-type', (headers, expected) => { | ||
expect(getEncoding(new Headers(headers))).toBe(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { Headers } from '@mastojs/ponyfills'; | ||
|
||
import type { Encoding } from '../serializers'; | ||
|
||
export const getEncoding = (headers: Headers): Encoding | undefined => { | ||
const contentType = headers.get('Content-Type')?.replace(/\s*;.*$/, ''); | ||
if (typeof contentType !== 'string') { | ||
return; | ||
} | ||
|
||
switch (contentType) { | ||
case 'application/json': { | ||
return 'json'; | ||
} | ||
case 'application/x-www-form-urlencoded': { | ||
return 'form-url-encoded'; | ||
} | ||
case 'multipart/form-data': { | ||
return 'multipart-form'; | ||
} | ||
default: { | ||
return; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.