Skip to content

Commit

Permalink
feat: refactor cookies to separate multiple from single type
Browse files Browse the repository at this point in the history
  • Loading branch information
awinogrodzki committed Sep 29, 2024
1 parent 78b0d66 commit 9aba786
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 16 deletions.
10 changes: 3 additions & 7 deletions src/next/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {NextApiRequest, NextApiResponse} from 'next';
import {CustomTokens, VerifiedTokens} from '../auth/custom-token/index.js';
import {getFirebaseAuth} from '../auth/index.js';
import {AuthCookies} from './cookies/AuthCookies.js';
import {SetAuthCookiesOptions} from './cookies/index.js';
import {CookiesObject, SetAuthCookiesOptions} from './cookies/index.js';
import {ObjectCookiesProvider} from './cookies/parser/ObjectCookiesProvider.js';
import {getCookiesTokens} from './tokens.js';

Expand All @@ -23,9 +23,7 @@ export async function refreshApiResponseCookies(
}

export async function appendAuthCookiesApi(
cookies: Partial<{
[key: string]: string;
}>,
cookies: CookiesObject,
response: NextApiResponse,
tokens: CustomTokens,
options: SetAuthCookiesOptions
Expand All @@ -39,9 +37,7 @@ export async function appendAuthCookiesApi(
}

export async function refreshApiCookies(
cookies: Partial<{
[key: string]: string;
}>,
cookies: CookiesObject,
headers: IncomingHttpHeaders,
options: SetAuthCookiesOptions
): Promise<VerifiedTokens> {
Expand Down
4 changes: 1 addition & 3 deletions src/next/cookies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ export function removeAuthCookies(
}

export async function verifyApiCookies(
cookies: Partial<{
[key: string]: string;
}>,
cookies: CookiesObject,
headers: IncomingHttpHeaders,
options: SetAuthCookiesOptions
): Promise<VerifiedTokens> {
Expand Down
6 changes: 2 additions & 4 deletions src/next/cookies/parser/CookieParserFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {RequestCookies} from 'next/dist/server/web/spec-extension/cookies';
import {InvalidTokenError, InvalidTokenReason} from '../../../auth/error.js';
import {debug} from '../../../debug/index.js';
import {GetCookiesTokensOptions} from '../../tokens.js';
import {CookiesObject} from '../index.js';
import {CookiesProvider} from './CookiesProvider.js';
import {MultipleCookiesParser} from './MultipleCookiesParser.js';
import {ObjectCookiesProvider} from './ObjectCookiesProvider.js';
Expand Down Expand Up @@ -105,10 +106,7 @@ export class CookieParserFactory {
return CookieParserFactory.fromProvider(provider, options);
}

static fromObject(
cookies: Partial<{[K in string]: string}>,
options: GetCookiesTokensOptions
) {
static fromObject(cookies: CookiesObject, options: GetCookiesTokensOptions) {
const provider = new ObjectCookiesProvider(cookies);

return CookieParserFactory.fromProvider(provider, options);
Expand Down
4 changes: 3 additions & 1 deletion src/next/cookies/parser/ObjectCookiesProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {CookiesObject} from '../index.js';

export class ObjectCookiesProvider {
constructor(private cookies: Partial<{[K in string]: string}>) {}
constructor(private cookies: CookiesObject) {}

get(key: string) {
return this.cookies[key];
Expand Down
2 changes: 1 addition & 1 deletion src/next/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export async function getTokens(
}

export async function getCookiesTokens(
cookies: Partial<{[K in string]: string}>,
cookies: CookiesObject,
options: GetCookiesTokensOptions
): Promise<CustomTokens> {
const parser = CookieParserFactory.fromObject(cookies, options);
Expand Down

0 comments on commit 9aba786

Please sign in to comment.