Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with serverless-esbuild #92

Open
kaykhan opened this issue Sep 15, 2022 · 4 comments
Open

Not working with serverless-esbuild #92

kaykhan opened this issue Sep 15, 2022 · 4 comments

Comments

@kaykhan
Copy link

kaykhan commented Sep 15, 2022

I'm trying this plugin out as i have a function id like to build swagger docs for. However there is no endpoint being created. I wonder if this works with serverless-ebuild. serverless-esbuild is whats used in the latset aws-nodejs-typescript template. This project was bootstrapped using serverless create --template aws-nodejs-typescript --path api

serverless.ts

import type { AWS } from "@serverless/typescript";

import { CollectionsHandler } from "@functions/collections";

const serverlessConfiguration: AWS = {
    service: "api",
    frameworkVersion: "3",
    plugins: ["serverless-auto-swagger", "serverless-esbuild", "serverless-offline", "serverless-domain-manager"],
    useDotenv: true,
    provider: {
        name: "aws",
        region: "us-east-2",
        runtime: "nodejs16.x",
        apiGateway: {
            minimumCompressionSize: 1024,
            shouldStartNameWithService: true,
            apiKeys: [{ name: "${self:service}-${sls:stage}", description: "api key" }],
        },
        environment: {
            ...
        },
        vpc: {...},
        iam: {
            role: {
                statements: [
                    {
                        Effect: "Allow",
                        Action: ["lambda:InvokeFunction"],
                        Resource: "*",
                    },
                ],
            },
        },
    },
    // import the function via paths
    functions: {
        ...CollectionsHandler,
    },
    package: {
        individually: true,
        patterns: [
            "!node_modules/.prisma/client/libquery_engine-*",
            "node_modules/.prisma/client/schema.prisma",
            "!node_modules/prisma/libquery_engine-*",
            "!node_modules/@prisma/engines/**",
            "node_modules/.prisma/client/libquery_engine-${env:PRISMA_BINARY_TARGET}-*",
        ],
    },

    custom: {
        esbuild: {
            bundle: true,
            minify: false,
            sourcemap: true,
            exclude: ["aws-sdk"],
            target: "node16",
            define: { "require.resolve": undefined },
            platform: "node",
            concurrency: 10,
        },
        stage: "${sls:stage}",
        domains: {...},
        customDomain: {....},
    },
};

module.exports = serverlessConfiguration;

CollectionsHandler

import type { AWS } from "@serverless/typescript";
import { handlerPath } from "@src/libs/handler-resolver";
import { CreateCollectionJSONSchema } from "./schema";

export const CollectionsHandler: AWS["functions"] = {
    "create-collection": {
        handler: `${handlerPath(__dirname)}/controller.CreateCollectionController`,
        timeout: 300,
        events: [
            {
                http: {
                    method: "post",
                    path: "collections",
                    request: {
                        schemas: {
                            "application/json": CreateCollectionJSONSchema,
                        },
                    },
                    private: true,
                },
            },
        ],
    },
};

When i run sls offline --stage dev there is no /dev/swagger function/endpoint created

@bfaulk96
Copy link
Collaborator

Can you try swapping the serverless-esbuild plugin to before serverless-auto-swagger? I'm not really familiar with that plugin, but perhaps it needs to come first.

Also try adding "swagger/**" to your package.patterns

@trbarton
Copy link
Contributor

trbarton commented Nov 3, 2022

I've experienced the same Issue. Works fine when deploying to AWS but not with serverless offline.
I've found a work around though. I added my own functions to my serverless.ts that reference the handlers created by serverless-auto-swagger works both local and remote now.

export const swaggerUI = {
  name: "swagger-ui",
  handler: "swagger/swagger-html.handler",
  disableLogs: true,
  events: [
    {
      http: {
        method: "get",
        path: "swagger-ui",
      },
    },
  ],
};
export const swaggerJSON = {
  name: "swagger-json",
  handler: "swagger/swagger-json.handler",
  disableLogs: true,
  events: [
    {
      http: {
        method: "get",
        path: "swagger-ui.json",
      },
    },
  ],
};

@traoreak
Copy link

@kaykhan @trbarton you have to add the start to the offline command for it to work. It's not super clearly said in the docs but that's the issue I had. serverless offline start --stage dev

@bfaulk96
Copy link
Collaborator

Hmm. That's good to know. The documentation that I sort of inherited said this was specific to Serverless 2, but seeing as you are using Serverless 3 I'll have to update those docs at some point. Thanks! Would you mind if I close this as there is a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants