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

CORS config TS types (both here and in @types) don't match the schema #49

Open
justingrant opened this issue Aug 19, 2021 · 5 comments
Open

Comments

@justingrant
Copy link

I ran into serverless/serverless#9858 because the TS types don't have an accurate type for HttpCors.

Here's the schema: https://github.com/serverless/serverless/blob/ef5a8faf13a8fbf8564e7c0621e88d1ea5357ea5/lib/plugins/aws/package/compile/events/apiGateway/index.js#L77-L98

The current type in this repo is essentially a no-op.

typescript/index.d.ts

Lines 190 to 199 in b700b72

cors?:
| boolean
| (
| {
[k: string]: unknown;
}
| {
[k: string]: unknown;
}
);

The current type in @types/serverless is incomplete (missing origin and methods properties) and inaccurate (one of origin or origins is required, not optional, making it easy to run into serverless/serverless#9858). Also, all the optional properties have | undefined appended which AFAIK is redundant and unnecessary.

The actual types should be something like this:

type HttpCors = 
(
  | { origins: string | string[]; origin?: undefined }
  | { origin: string; origins?: undefined }
) & {
  headers?: string | string[];
  allowCredentials?: boolean;
  maxAge?: number;
  cacheControl?: string;
  methods?: string[];
};

or this

interface HttpCorsBase {
  headers?: string | string[];
  allowCredentials?: boolean;
  maxAge?: number;
  cacheControl?: string;
  methods?: string[];
};
interface HttpCorsOrigin extends HttpCorsBase {
  origin: string;
  origins?: undefined;
}
interface HttpCorsOrigins extends HttpCorsBase {
  origins: string | string[];
  origin?: undefined;
}
interface Http {
  path: string;
  method: string;
  cors?: boolean | HttpCorsOrigin | HttpCorsOrigins | undefined;
  ...
@fredericbarthelet
Copy link
Contributor

Thanks for pointing out this issue @justingrant .
Types from this repository are auto-generated from schema located in https://github.com/serverless/serverless. The library handling the type generation is struggling with the oneOf keyword located in the required section of the CORS schema. Once it is removed, chances are next type generation will be much more accurate.

I tried removing this oneOf property to give it a try and actually got correctly generated types
image

@justingrant
Copy link
Author

I tried removing this oneOf property to give it a try and actually got correctly generated types

Those types don't actually look correct:

@justingrant justingrant changed the title CORS config TS types (both here and in @types) don't mach the schema CORS config TS types (both here and in @types) don't match the schema Sep 1, 2021
@ed-graham
Copy link

ed-graham commented Oct 18, 2021

I've also just come across this. In my case, I'm interested in the exposedResponseHeaders property that is available in the YAML configuration but not in the TypeScript equivalent:

provider:
  httpApi:
    cors:
      allowedOrigins:
        - https://url1.com
        - https://url2.com
      allowedHeaders:
        - Content-Type
        - Authorization
      allowedMethods:
        - GET
      allowCredentials: true
      exposedResponseHeaders:
        - Special-Response-Header
      maxAge: 6000 # In seconds

(from https://www.serverless.com/blog/aws-http-api-support#configuring-cors).

I notice that exposedResponseHeaders is not listed as a valid property in the CORS schema. Is there a reason for that?

@fredericbarthelet
Copy link
Contributor

Hi @ed-graham, the exposedResponseHeaders property is available in the typescript definition from this repository in

exposedResponseHeaders?: string[];

The documentation you're referring to is for httpApi event types, that are provisioned with API Gateway HTTP API.
However the code base containing CORS schema you're referring to is for http event types, that are provisioned with API Gateway Rest API. Those are not the same and does not share the same capabilities.

@ed-graham
Copy link

Thanks @fredericbarthelet - that's really helpful! I didn't realise I had the two things mixed up. It turns out that I was still using the old community-maintained Definitely Typed package rather than your new one: so now I've upgraded and everything is working as expected.

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

3 participants