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

make type definitons "module": "nodenext" compatible #184

Merged
merged 11 commits into from
Jul 22, 2022
96 changes: 63 additions & 33 deletions plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ declare module 'fastify' {
export type setCookieWrapper = (
name: string,
value: string,
options?: CookieSerializeOptions
options?: fastifyCookie.CookieSerializeOptions
) => FastifyReply;

interface FastifyReply {
Expand All @@ -63,20 +63,27 @@ declare module 'fastify' {
setCookie(
name: string,
value: string,
options?: CookieSerializeOptions
options?: fastifyCookie.CookieSerializeOptions
): this;

/**
* @alias setCookie
*/
cookie(name: string, value: string, options?: CookieSerializeOptions): this;
cookie(
name: string,
value: string,
options?: fastifyCookie.CookieSerializeOptions
): this;

/**
* clear response cookie
* @param name Cookie name
* @param options Serialize options
*/
clearCookie(name: string, options?: CookieSerializeOptions): this;
clearCookie(
name: string,
options?: fastifyCookie.CookieSerializeOptions
): this;

/**
* Unsigns the specified cookie using the secret provided.
Expand All @@ -90,38 +97,61 @@ declare module 'fastify' {
}
}

export interface CookieSerializeOptions {
domain?: string;
encode?(val: string): string;
expires?: Date;
httpOnly?: boolean;
maxAge?: number;
path?: string;
priority?: 'low' | 'medium' | 'high';
sameSite?: boolean | 'lax' | 'strict' | 'none';
secure?: boolean;
signed?: boolean;
}
type FastifyCookiePlugin = FastifyPluginCallback<
NonNullable<fastifyCookie.FastifyCookieOptions>
>;

interface Signer {
sign: (input: string) => string;
unsign: (input: string) => {
valid: boolean;
renew: boolean;
value: string | null;
};
}
declare namespace fastifyCookie {
export interface Signer {
sign: (input: string) => string;
unsign: (input: string) => {
valid: boolean;
renew: boolean;
value: string | null;
};
}

export interface CookieSerializeOptions {
domain?: string;
encode?(val: string): string;
expires?: Date;
httpOnly?: boolean;
maxAge?: number;
path?: string;
priority?: "low" | "medium" | "high";
sameSite?: boolean | "lax" | "strict" | "none";
secure?: boolean;
signed?: boolean;
}

export interface FastifyCookieOptions {
secret?: string | string[] | Signer;
parseOptions?: fastifyCookie.CookieSerializeOptions;
}

declare const signerFactory: Signer;
declare const sign: (value: string, secret: string) => string;
declare const unsign: (input: string, secret: string) => string | false;
export const signerFactory: Signer;
export const sign: (value: string, secret: string) => string;
export const unsign: (input: string, secret: string) => string | false;

interface FastifyCookieAdditionalProperties {
signerFactory: typeof signerFactory;
sign: typeof sign;
unsign: typeof unsign;
}

export const fastifyCookie: FastifyCookiePlugin &
FastifyCookieAdditionalProperties;

export interface FastifyCookieOptions {
secret?: string | string[] | Signer;
parseOptions?: CookieSerializeOptions;
}

export interface FastifyCookieOptions {
secret?: string | string[] | Signer;
parseOptions?: CookieSerializeOptions;
export { fastifyCookie as default };
}

declare const fastifyCookie: FastifyPluginCallback<NonNullable<FastifyCookieOptions>>;
declare function fastifyCookie(
...params: Parameters<FastifyCookiePlugin>
): ReturnType<FastifyCookiePlugin>;

export default fastifyCookie;
export { fastifyCookie, signerFactory, sign, unsign };
export = fastifyCookie;
23 changes: 16 additions & 7 deletions test/plugin.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ import { Server } from 'http';
import { expectType } from 'tsd';
import * as fastifyCookieStar from '..';
import fastifyCookieDefault, {
fastifyCookie as fastifyCookieNamed
fastifyCookie as fastifyCookieNamed,
signerFactory,
sign,
unsign
} from '..';
import cookie, { FastifyCookieOptions } from '../plugin';

interface FastifyCookieAdditionalProperties {
signerFactory: typeof signerFactory;
sign: typeof sign;
unsign: typeof unsign;
}

import fastifyCookieCjsImport = require('..');
const fastifyCookieCjs = require('..');

Expand All @@ -19,12 +28,12 @@ app.register(fastifyCookieCjsImport.fastifyCookie);
app.register(fastifyCookieStar.default);
app.register(fastifyCookieStar.fastifyCookie);

expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(fastifyCookieNamed);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(fastifyCookieDefault);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(fastifyCookieCjsImport.default);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(fastifyCookieCjsImport.fastifyCookie);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(fastifyCookieStar.default);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server>>(
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(fastifyCookieNamed);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(fastifyCookieDefault);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(fastifyCookieCjsImport.default);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(fastifyCookieCjsImport.fastifyCookie);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(fastifyCookieStar.default);
expectType<FastifyPluginCallback<FastifyCookieOptions, Server> & FastifyCookieAdditionalProperties>(
fastifyCookieStar.fastifyCookie
);
expectType<any>(fastifyCookieCjs);
Expand Down