@asfweb/grpc-session
Version:
## Installation: ``` yarn add @asfweb/grpc-session ``` or ``` npm install @asfweb/grpc-session --save ```
119 lines (118 loc) • 2.69 kB
TypeScript
import { Metadata } from "@grpc/grpc-js";
import cookie from "cookie";
import { ServerSurfaceCall } from "@grpc/grpc-js/build/src/server-call";
import { Store } from "./Store";
/**
* Session Error Class
*/
export declare class SessionError extends Error {
private __proto__?;
constructor(message?: string);
}
/**
* Session Options Interface
*/
export interface SessionOptions {
expires?: number;
sessionName?: string;
checkOrigin?: boolean;
cookie?: cookie.CookieSerializeOptions;
debug?: boolean;
}
export declare type Primitive = string | number | boolean | null;
export declare type SessionKeyValue = Primitive | {
[key: string]: Primitive;
};
export declare type SessionData = {
[key: string]: SessionKeyValue;
} | null | any;
/**
* Session class
*/
export declare class Session {
private metadata;
private sessionData;
private sessionName;
private sessionId;
private store;
options: SessionOptions;
/**
* Session
*
* @param store Session Store
* @param options {sessionName:"_SID", expires: "Time in seconds: 60*60*20"}
*/
constructor(store: Store, options?: SessionOptions);
/**
* Creates new session
*
* @param sessionData
* @returns Session
*/
private start;
/**
* Will try to restore session from id or will create a new one
*
* @param call ServerSurfaceCall
* @returns Promise<Session>
*/
gRPC(call: ServerSurfaceCall, sessionData?: SessionData): Promise<this>;
/**
* Get session key
*
* @param key string key
* @returns string
* @throws SessionError
*/
get<T>(key?: string): T;
/**
* Sets new session key
*
* @param key string
* @param value string
* @returns Session
* @throws SessionError
*/
set(key: string, value: SessionKeyValue): this;
/**
* Removes key from session
*
* @param key string
* @returns Session
* @throws SessionError
*/
remove(key: string): this;
/**
* Gets session id
*
* @returns string
* @throws SessionError
*/
id(): string;
/**
* Get Grpc Metadata
*
* @returns Metadata
* @throws SessionError
*/
getMetadata(): Metadata;
/**
* Saves Session
*
* @returns Promise<boolean>
* @throws SessionError
*/
save(): Promise<boolean>;
/**
* Deletes Session
*
* @returns Promise<boolean>
*/
destroy(): boolean | Promise<boolean>;
/**
* A md5 hash of sessionId : user-agent header
*
* @returns string
*/
private _MD5_hash;
}