npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-cookie-session

v3.0.1

Published

Idiomatic NestJS module for cookie session

Downloads

3,490

Readme

This module implements a session with storing data directly in Cookie.

If you want to store data in one of external stores and passing ID of session to client via Cookie/Set-Cookie headers, you can look at nestjs-session.

Example

Register module:

// app.module.ts
import { Module } from '@nestjs/common';
import {
  NestCookieSessionOptions,
  CookieSessionModule,
} from 'nestjs-cookie-session';
import { ViewsController } from './views.controller';

@Module({
  imports: [
    // sync params:

    CookieSessionModule.forRoot({
      session: { secret: 'keyboard cat' },
    }),

    // or async:

    CookieSessionModule.forRootAsync({
      imports: [ConfigModule],
      inject: [Config],
      //              TIP: to get autocomplete in return object
      //                  add `NestCookieSessionOptions` here ↓↓↓
      useFactory: async (config: Config): Promise<NestCookieSessionOptions> => {
        return {
          session: { secret: config.secret },
        };
      },
    }),
  ],
  controllers: [ViewsController],
})
export class AppModule {}

Use in controllers with NestJS built-in Session decorator:

// views.controller.ts
import { Controller, Get, Session } from '@nestjs/common';

@Controller('views')
export class ViewsController {
  @Get()
  getViews(@Session() session: { views?: number }) {
    session.views = (session.views || 0) + 1;
    return session.views;
  }
}

To run examples:

git clone https://github.com/iamolegga/nestjs-cookie-session.git
cd nestjs-cookie-session
npm i
npm run build
cd example
npm i
npm start

Install

npm i nestjs-cookie-session cookie-session @types/cookie-session

API

CookieSessionModule

CookieSessionModule class has two static methods, that returns DynamicModule, that you need to import:

  • CookieSessionModule.forRoot for sync configuration without dependencies
  • CookieSessionModule.forRootAsync for sync/async configuration with dependencies

CookieSessionModule.forRoot

Accept NestCookieSessionOptions. Returns NestJS DynamicModule for import.

CookieSessionModule.forRootAsync

Accept NestCookieSessionAsyncOptions. Returns NestJS DynamicModule for import.

NestCookieSessionOptions

NestCookieSessionOptions is the interface of all options, has next properties:

  • session - required - cookie-session options.
  • forRoutes - optional - same as NestJS built-in MiddlewareConfigProxy['forRoutes'] See examples in official docs. Specify routes, that should have access to session. If forRoutes and exclude will not be set, then sessions will be set to all routes.
  • exclude - optional - same as NestJS built-in MiddlewareConfigProxy['exclude'] See examples in official docs. Specify routes, that should not have access to session. If forRoutes and exclude will not be set, then sessions will be set to all routes.

NestCookieSessionAsyncOptions

NestCookieSessionOptions is the interface of options to create cookie session module, that depends on other modules, has next properties:

  • imports - optional - modules, that cookie session module depends on. See official docs.
  • inject - optional - providers from imports-property modules, that will be passed as arguments to useFactory method.
  • useFactory - required - method, that returns NestCookieSessionOptions.

Migration

v2

cookie-session and @types/cookie-session are moved to peer dependencies, so you can update them independently.