UNPKG

@piggly/fastify-chassis

Version:

An ESM/CommonJS toolkit to help you to do common operations in your back-end applications with Fastify and NodeJS.

144 lines (143 loc) 4.32 kB
import { EnvironmentType, TOrUndefined } from '@piggly/ddd-toolkit'; import type { FastifyRequest, FastifyReply } from 'fastify'; /** * Cookie builder service settings. * * @property {string} domain The domain of the cookie. * @property {EnvironmentType} environment The environment of the cookie. * @since 7.0.0 * @author Caique Araujo <caique@piggly.com.br> */ export type CookieBuilderServiceSettings = { domain: string; environment: EnvironmentType; }; /** * Cookie options. * * @property {string} [domain] The domain of the cookie. * @property {function(string): string} [encode] The function to encode the cookie value. * @property {Date} [expires] The expiration date of the cookie. * @property {boolean} [httpOnly] Whether the cookie is http only. * @property {number} [maxAge] The max age of the cookie. * @property {boolean} [partitioned] Whether the cookie is partitioned. * @property {string} [path] The path of the cookie. * @property {string} [priority] The priority of the cookie. * @property {string} [sameSite] The same site of the cookie. * @property {boolean} [secure] Whether the cookie is secure. * @property {boolean} [signed] Whether the cookie is signed. * @since 7.0.0 * @author Caique Araujo <caique@piggly.com.br> */ export type CookieOptions = { domain?: string; encode?(val: string): string; expires?: Date; httpOnly?: boolean; maxAge?: number; partitioned?: boolean; path?: string; priority?: 'medium' | 'high' | 'low'; sameSite?: 'strict' | boolean | 'none' | 'lax'; secure?: boolean; signed?: boolean; }; /** * @file Cookie builder service. * @copyright Piggly Lab 2024 */ export declare class CookieBuilderService { /** * Settings. * * @type {CookieBuilderServiceSettings} * @protected * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ protected _settings: CookieBuilderServiceSettings; /** * Constructor. * * @public * @constructor * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ constructor(settings: CookieBuilderServiceSettings); /** * Clear cookie. * * @param {FastifyReply} reply * @param {string} name * @public * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ clear(reply: FastifyReply, name: string): void; /** * Get cookie value. * * @param {FastifyRequest} request * @param {string} name * @param {string} default_value * @returns {TOrUndefined<string>} * @public * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ get(request: FastifyRequest, name: string, default_value?: string): TOrUndefined<string>; /** * Check if has cookie. * * @param {FastifyRequest} request * @param {string} name * @returns {boolean} * @public * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ has(request: FastifyRequest, name: string): boolean; /** * Register application service. * * @param {FastifyReply} reply * @param {string} name * @param {string} value * @param {CookieOptions} options * @public * @static * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ set(reply: FastifyReply, name: string, value: string, options?: CookieOptions): void; /** * Register application service. * * @param {CookieBuilderService} service * @public * @static * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ static register(service: CookieBuilderService): void; /** * Resolve application service. * * @returns {CookieBuilderService} * @throws {Error} If service is not registered. * @public * @static * @memberof CookieBuilderService * @since 5.1.0 * @author Caique Araujo <caique@piggly.com.br> */ static resolve(): CookieBuilderService; }