UNPKG

@ima/core

Version:

IMA.js framework for isomorphic javascript application

183 lines 6.53 kB
import { Storage } from './Storage'; import { Dependencies } from '../oc/ObjectContainer'; import { Request } from '../router/Request'; import { Response } from '../router/Response'; import { Window } from '../window/Window'; export type CookieOptions = { domain?: string; expires?: Date; httpOnly?: boolean; maxAge?: number; path?: string; sameSite?: 'none' | 'lax' | 'strict' | undefined; secure?: boolean; partitioned?: boolean; }; export type Cookie = { options: CookieOptions; value: string | number | boolean | Date | undefined; }; /** * Storage of cookies, mirroring the cookies to the current request / response * at the server side and the `document.cookie` property at the client * side. The storage caches the cookies internally. */ export declare class CookieStorage extends Storage<Cookie['value']> { #private; /** * The window utility used to determine whether the IMA is being run * at the client or at the server. */ private _window; /** * The current HTTP request. This field is used at the server side. */ private _request; /** * The current HTTP response. This field is used at the server side. */ private _response; /** * The internal storage of entries. */ private _storage; /** * The overriding cookie attribute values. */ private _options; /** * Transform encode and decode functions for cookie value. */ private _transformFunction; static get $dependencies(): Dependencies; /** * Filters invalid cookies based on the provided url. * We try to check validity of the domain based on secure, path and * domain definitions. */ static validateCookieSecurity(cookie: Cookie, url: string): boolean; /** * Initializes the cookie storage. * * @param window The window utility. * @param request The current HTTP request. * @param response The current HTTP response. * @example * cookie.set('cookie', 'value', { expires: 10 }); // cookie expires * // after 10s * cookie.set('cookie'); // delete cookie * */ constructor(window: Window, request: Request, response: Response); /** * @inheritDoc */ init(options?: CookieOptions, transformFunction?: {}): this; /** * @inheritDoc */ has(name: string): boolean; /** * @inheritDoc */ get(name: string): Cookie['value']; /** * @inheritDoc * @param name The key identifying the storage entry. * @param value The storage entry value. * @param options The cookie options. The `maxAge` is the maximum * age in seconds of the cookie before it will be deleted, the * `expires` is an alternative to that, specifying the moment * at which the cookie will be discarded. The `domain` and * `path` specify the cookie's domain and path. The * `httpOnly` and `secure` flags set the flags of the * same name of the cookie. */ set(name: string, value: Cookie['value'], options?: CookieOptions): this; /** * Deletes the cookie identified by the specified name. * * @param name Name identifying the cookie. * @param options The cookie options. The `domain` and * `path` specify the cookie's domain and path. The * `httpOnly` and `secure` flags set the flags of the * same name of the cookie. * @return This storage. */ delete(name: string, options?: CookieOptions): this; /** * @inheritDoc */ clear(): this; /** * @inheritDoc */ keys(): Iterable<string>; /** * @inheritDoc */ size(): number; /** * Returns all cookies in this storage serialized to a string compatible * with the `Cookie` HTTP header. * * When `url` is provided, the method validates the cookie security based on * the `url` and the cookie's domain, path, and secure attributes. * * @return All cookies in this storage serialized to a string * compatible with the `Cookie` HTTP header. */ getCookiesStringForCookieHeader(url?: string): string; /** * Parses cookies from the provided `Set-Cookie` HTTP header value. * * When `url` is provided, the method validates the cookie security based on * the `url` and the cookie's domain, path, and secure attributes. * * The parsed cookies will be set to the internal storage, and the current * HTTP response (via the `Set-Cookie` HTTP header) if at the server * side, or the browser (via the `document.cookie` property). * * @param cookiesString The value of the `Set-Cookie` HTTP * header. When there are multiple cookies, the value can be * provided as an array of strings. */ parseFromSetCookieHeader(cookiesString: string | string[], url?: string): void; /** * Parses cookies from a cookie string and sets the parsed cookies to the * internal storage. * * The method obtains the cookie string from the request's `Cookie` * HTTP header when used at the server side, and the `document.cookie` * property at the client side. */ parse(): void; /** * Sanitize cookie value by rules in * (@see http://tools.ietf.org/html/rfc6265#section-4r.1.1). Erase all * invalid characters from cookie value. * * @param value Cookie value * @return Sanitized value */ sanitizeCookieValue(value: Cookie['value']): string; /** * Recomputes cookie's attributes maxAge and expires between each other. * * @param options Cookie attributes. Only the attributes listed in the * type annotation of this field are supported. For documentation * and full list of cookie attributes see * http://tools.ietf.org/html/rfc2965#page-5 */ recomputeCookieMaxAgeAndExpires(options: CookieOptions): void; /** * Converts the provided cookie expiration to a `Date` instance. * * @param expiration Cookie expiration in seconds * from now, or as a string compatible with the `Date` * constructor. * @return Cookie expiration as a `Date` instance. */ getExpirationAsDate(expiration: number | string | Date): Date; } //# sourceMappingURL=CookieStorage.d.ts.map