-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: support edge runtimes * chore: replace jsonwebtoken with edge-compatible jose package * fix: throw on non-2xx responses and improve data preparation * fix: update signuserToken return type * fix: jwt signing by importing token as a KeyLike * update example in readme * chore: prepare for 0.6.0 release
- Loading branch information
1 parent
9060680
commit 2978aff
Showing
7 changed files
with
1,232 additions
and
1,151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
## v0.6.0 | ||
|
||
### Major Changes | ||
|
||
- Add Vercel Edge runtime compatibility | ||
|
||
### Breaking Changes | ||
|
||
- `Knock.signUserToken` is now asynchronous and returns `Promise<string>` instead of `string` | ||
|
||
## v0.4.18 | ||
|
||
* Introduce "Idempotency-Key" header for workflow triggers | ||
- Introduce "Idempotency-Key" header for workflow triggers |
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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
export interface FetchClientConfig { | ||
baseURL?: string; | ||
headers?: Record<string, string>; | ||
} | ||
|
||
export interface FetchRequestConfig<D = any> { | ||
params?: Record<string, string>; | ||
headers?: Record<string, string>; | ||
body?: D; | ||
} | ||
|
||
export interface FetchResponse<T = any> extends Response { | ||
data: T; | ||
} | ||
|
||
export class FetchResponseError extends Error { | ||
readonly response: FetchResponse; | ||
|
||
constructor(response: FetchResponse) { | ||
super(); | ||
this.response = response; | ||
} | ||
} | ||
|
||
const defaultConfig: FetchClientConfig = { | ||
baseURL: "", | ||
headers: {}, | ||
}; | ||
|
||
export default class FetchClient { | ||
config: FetchClientConfig; | ||
|
||
constructor(config?: FetchClientConfig) { | ||
this.config = { | ||
...defaultConfig, | ||
...config, | ||
}; | ||
} | ||
|
||
async get(path: string, config: FetchRequestConfig): Promise<FetchResponse> { | ||
return this.request("GET", path, config); | ||
} | ||
|
||
async post(path: string, config: FetchRequestConfig): Promise<FetchResponse> { | ||
return this.request("POST", path, config); | ||
} | ||
|
||
async put(path: string, config: FetchRequestConfig): Promise<FetchResponse> { | ||
return this.request("PUT", path, config); | ||
} | ||
|
||
async delete( | ||
path: string, | ||
config: FetchRequestConfig, | ||
): Promise<FetchResponse> { | ||
return this.request("DELETE", path, config); | ||
} | ||
|
||
private async request( | ||
method: string, | ||
path: string, | ||
config: FetchRequestConfig = {}, | ||
): Promise<FetchResponse> { | ||
const url = this.buildUrl(path, config.params); | ||
const headers = { | ||
...this.config.headers, | ||
...(config.headers ?? {}), | ||
}; | ||
|
||
const response = await fetch(url, { | ||
method, | ||
headers, | ||
body: config.body ? this.prepareRequestBody(config.body) : undefined, | ||
}); | ||
const data = await this.getResponseData(response); | ||
|
||
// Assign data to the response as other methods of returning the response | ||
// like return { ...response, data } drop the response methods | ||
const fetchResponse: FetchResponse = Object.assign(response, { | ||
data, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new FetchResponseError(fetchResponse); | ||
} | ||
|
||
return fetchResponse; | ||
} | ||
|
||
private buildUrl(path: string, params?: FetchRequestConfig["params"]): URL { | ||
const url = new URL(this.config.baseURL + path); | ||
|
||
if (params) { | ||
Object.entries(params).forEach(([key, value]) => | ||
url.searchParams.append(key, value), | ||
); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
private prepareRequestBody(data: any): string | FormData { | ||
if (typeof data === "string" || data instanceof FormData) { | ||
return data; | ||
} | ||
return JSON.stringify(data); | ||
} | ||
|
||
private async getResponseData(response: Response) { | ||
if (!response.body) { | ||
return undefined; | ||
} | ||
|
||
let data; | ||
const contentType = response.headers.get("content-type"); | ||
if (contentType && contentType.includes("application/json")) { | ||
data = await response.json(); | ||
} else if (contentType && contentType.includes("text")) { | ||
data = await response.text(); | ||
} else { | ||
data = await response.blob(); | ||
} | ||
|
||
return data; | ||
} | ||
} |
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
Oops, something went wrong.