Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vanilla adapter (prerelease v3.4.0) #144

Merged
merged 9 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <jason@raimondi.us>",
"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,
});
}
Loading