-
-
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.
Merge pull request #144 from jasonraimondi/next
feat: vanilla adapter
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@jmondi/oauth2-server", | ||
"version": "3.3.1", | ||
"version": "3.4.0-next.3", | ||
"type": "module", | ||
"author": "Jason Raimondi <[email protected]>", | ||
"funding": "https://github.com/sponsors/jasonraimondi", | ||
|
@@ -19,6 +19,7 @@ | |
}, | ||
"exports": { | ||
".": "./src/index.ts", | ||
"./vanilla": "./src/adapters/vanilla.ts", | ||
"./express": "./src/adapters/express.ts", | ||
"./fastify": "./src/adapters/fastify.ts" | ||
}, | ||
|
@@ -32,6 +33,11 @@ | |
"require": "./dist/index.cjs", | ||
"types": "./dist/index.d.ts" | ||
}, | ||
"./vanilla": { | ||
"import": "./dist/vanilla.js", | ||
"require": "./dist/vanilla.cjs", | ||
"types": "./dist/vanilla.d.ts" | ||
}, | ||
"./express": { | ||
"import": "./dist/express.js", | ||
"require": "./dist/express.cjs", | ||
|
@@ -88,6 +94,7 @@ | |
"tsup": { | ||
"entry": { | ||
"index": "./src/index.ts", | ||
"vanilla": "./src/adapters/vanilla.ts", | ||
"express": "./src/adapters/express.ts", | ||
"fastify": "./src/adapters/fastify.ts" | ||
}, | ||
|
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,57 @@ | ||
import { OAuthRequest } from "../requests/request.js"; | ||
import { OAuthResponse } from "../responses/response.js"; | ||
import { ErrorType, OAuthException } from "../exceptions/oauth.exception.js"; | ||
|
||
export function responseFromVanilla(res: Response): OAuthResponse { | ||
const headers: Record<string, unknown> = {}; | ||
Object.entries(res.headers).forEach(([key, value]) => { | ||
headers[key] = value; | ||
}); | ||
|
||
return new OAuthResponse({ | ||
headers: headers, | ||
}); | ||
} | ||
|
||
export function requestFromVanilla(req: Request): OAuthRequest { | ||
const url = new URL(req.url); | ||
const query: Record<string, unknown> = {}; | ||
url.searchParams.forEach((value, key) => { | ||
query[key] = value; | ||
}); | ||
|
||
let body: Record<string, unknown> = {}; | ||
if (req.body != null) { | ||
body = JSON.parse(req.body.toString()); | ||
} | ||
|
||
const headers: Record<string, unknown> = {}; | ||
Object.entries(req.headers).forEach(([key, value]) => { | ||
headers[key] = value; | ||
}); | ||
|
||
return new OAuthRequest({ | ||
query: query, | ||
body: body, | ||
headers: headers, | ||
}); | ||
} | ||
|
||
export function responseToVanilla(oauthResponse: OAuthResponse): Response { | ||
if (oauthResponse.status === 302) { | ||
if (!oauthResponse.headers.location) { | ||
throw new OAuthException(`missing redirect location`, ErrorType.InvalidRequest); | ||
} | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: oauthResponse.headers.location, | ||
}, | ||
}); | ||
} | ||
|
||
return new Response(JSON.stringify(oauthResponse.body), { | ||
status: oauthResponse.status, | ||
headers: oauthResponse.headers, | ||
}); | ||
} |