LogoLogo
CS CONSOLECOMMUNITYDOCS
  • What is code.store?
  • Our vision
  • How it works?
  • Getting started
    • Quick Start
      • Core concepts
      • Quick Start with CLI
      • Quick Start with Web Interface
    • FUNDAMENTALS
      • Services
        • GraphQL
        • REST
        • Configuration
        • Environment Variables
      • Environments
      • Projects
      • Versioning
      • Access and Authorization
      • Secret management
      • Data management
        • Import and Export
      • Generation
        • Models
        • Handlers
        • Resolvers
        • REST
        • Middlewares
      • Logging
      • Microservices
        • Communication
    • RECIPES
      • TypeScript
        • Custom .tslint
      • Basic authentication
      • Setup a local database with your service
      • GraphQL
    • Tutorials
      • Auth0 authentication
      • Metabase integration
      • Database manipulations
        • External database: MongoDB
        • External database: DynamoDB
      • Wrapping an NPM package with GraphQL: a chess server
    • FAQ
  • Command Line Interface
    • Getting started
    • Commands
Powered by GitBook
On this page

Was this helpful?

Export as PDF
  1. Getting started
  2. FUNDAMENTALS
  3. Generation

Middlewares

PreviousRESTNextLogging

Last updated 4 years ago

Was this helpful?

Overview

code.store platform support functions for endpoints. To avoid manual file creation allowed a command cs generate:middleware, which generate a template of middleware with handler method.

To generate middleware for your route just execute cs generate:middleware CLI command, select one of middleware type: request or response middleware type, specify name for your function and select route from the route list:

> cs generate:middleware

? Select position of middleware (Use arrow keys)
  response 
❯ request 

? Enter name of your middleware: permissions

? Select route to apply (Use arrow keys)

File ./demo_app/src/rest-middlewares/permissions.middleware.ts has been generated.

Also, you can use flags:

-p for type specification (allowed: 'request' or 'response)

-r name of the route to apply middleware function

-n name of middleware function

For example, here a command from previous example: cs generate:middleware -p request -r hello -n permissions

After command execution will be generated file permissions.middleware.ts in src/rest-middlewares directory:

/**
 * This handler was generated by code.store CLI.
 *
 * Visit our documentation to learn more about the support of REST APIs in our SDK:
 * https://docs.code.store/getting-started/sdk/rest-apis
 *
 * You can also join our community at https://spectrum.chat/code-store if you have any
 * questions which are not covered by the documentation.
 *
 */

import { Handler } from 'codestore-utils';

const handler: Handler = async (event, context) => {
  // your code goes here
  return 'Hello, world!';
}

export default handler;

More information about handler method and his params can be found in section.

middleware
REST
REST