Skip to content

Commit

Permalink
Merge pull request #144 from jasonraimondi/next
Browse files Browse the repository at this point in the history
feat: vanilla adapter
  • Loading branch information
jasonraimondi authored Jul 5, 2024
2 parents c7052ab + e376727 commit 81a2810
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
3 changes: 2 additions & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"name": "@jmondi/oauth2-server",
"version": "3.3.1",
"version": "3.4.0-next.3",
"exports": {
".": "./src/index.ts",
"./vanilla": "./src/adapters/vanilla.ts",
"./express": "./src/adapters/express.ts",
"./fastify": "./src/adapters/fastify.ts",
"./vanilla": "./src/adapters/vanilla.ts"
Expand Down
9 changes: 8 additions & 1 deletion package.json
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",
Expand All @@ -19,6 +19,7 @@
},
"exports": {
".": "./src/index.ts",
"./vanilla": "./src/adapters/vanilla.ts",
"./express": "./src/adapters/express.ts",
"./fastify": "./src/adapters/fastify.ts"
},
Expand All @@ -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",
Expand Down Expand Up @@ -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"
},
Expand Down
57 changes: 57 additions & 0 deletions src/adapters/vanilla.ts
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,
});
}

0 comments on commit 81a2810

Please sign in to comment.