-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(clerk-sdk-node): Teach node-sdk to work with fetch
Polyfill global.fetch only in Node environments if necessary and increase interoperability for the SDK across Node and V8 runtime environments. Fixes #127
- Loading branch information
1 parent
13da4de
commit 369df5c
Showing
19 changed files
with
650 additions
and
717 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
@@ -1,22 +1,46 @@ | ||
import { ClerkBackendAPI } from '@clerk/backend-core'; | ||
import { ClerkAPIResponseError, ClerkBackendAPI } from '@clerk/backend-core'; | ||
|
||
import { PACKAGE_REPO } from '../constants'; | ||
import { LIB_NAME, LIB_VERSION } from '../info'; | ||
|
||
export const ClerkAPI = new ClerkBackendAPI({ | ||
libName: LIB_NAME, | ||
libVersion: LIB_VERSION, | ||
packageRepo: PACKAGE_REPO, | ||
fetcher: (url, { method, authorization, contentType, userAgent, body }) => { | ||
return fetch(url, { | ||
method, | ||
headers: { | ||
authorization: authorization, | ||
'Content-Type': contentType, | ||
'User-Agent': userAgent, | ||
'X-Clerk-SDK': `vercel-edge/${LIB_VERSION}`, | ||
}, | ||
...(body && { body: JSON.stringify(body) }), | ||
}).then(body => (contentType === 'text/html' ? body : body.json())); | ||
apiClient: { | ||
async request({ url, method, queryParams, headerParams, bodyParams }) { | ||
// Build final URL with search parameters | ||
const finalUrl = new URL(url || ''); | ||
|
||
if (queryParams) { | ||
for (const [key, val] of Object.entries(queryParams as Record<string, string | string[]>)) { | ||
// Support array values for queryParams such as { foo: [42, 43] } | ||
if (val) { | ||
[val].flat().forEach(v => finalUrl.searchParams.append(key, v)); | ||
} | ||
} | ||
} | ||
|
||
const response = await fetch(finalUrl.href, { | ||
method, | ||
headers: { | ||
...(headerParams as Record<string, string>), | ||
'X-Clerk-SDK': `vercel-edge/${LIB_VERSION}`, | ||
}, | ||
...(bodyParams && Object.keys(bodyParams).length > 0 && { body: JSON.stringify(bodyParams) }), | ||
}); | ||
|
||
// Parse JSON or Text response. | ||
const isJSONResponse = headerParams && headerParams['Content-Type'] === 'application/json'; | ||
const data = await (isJSONResponse ? response.json() : response.text()); | ||
|
||
// Check for errors | ||
if (!response.ok) { | ||
throw new ClerkAPIResponseError(response.statusText, { | ||
data: data?.errors || data, | ||
status: response.status, | ||
}); | ||
} | ||
|
||
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 was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
packages/edge/src/vercel-edge/utils/injectAuthIntoRequest.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.