Skip to content

Commit

Permalink
Merge pull request #81 from getlift/add-type-generation
Browse files Browse the repository at this point in the history
Add types definition for serverless.ts
  • Loading branch information
fredericbarthelet authored Jul 22, 2021
2 parents 53d46aa + 97f82b9 commit 5ad131b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ To eject:
- copy the parts you want to turn into CloudFormation and paste them in the [`resources` section of serverless.yml](https://www.serverless.com/framework/docs/providers/aws/guide/resources/)
- don't forget to remove from `serverless.yml` the Lift constructs you have turned into CloudFormation

## [Types definition for Lift constructs](docs/serverless-types.md)

---

Lift is built and maintained with love ❤️ by
Expand Down
35 changes: 35 additions & 0 deletions docs/serverless-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Lift Typescript definitions

While YAML remains the most popular declarative syntax for Serverless service file definition - a.k.a `serverless.yml` - you can also use Javascript/Typescript definitions - i.e. `serverless.js` and `serverless.ts`. You can find more information on using JS/TS service file in the [Serverless official documentation](https://www.serverless.com/framework/docs/providers/aws/guide/intro#services).

Generated Typescript types for the service file from [@serverless/typescript](https://github.com/serverless/typescript) do NOT include `constructs` definition from Lift.

In order to cope this issue, Lift exports a type definition you can use in conjonction with the official Serverless definition:

_serverless.ts_
```ts
import type { AWS } from '@serverless/typescript';
import type { Lift } from "serverless-lift";

const serverlessConfiguration: AWS & Lift = {
service: 'myService',
frameworkVersion: '2',
plugins: ['serverless-lift'],
constructs: {
avatars: {
type: 'storage',
},
},
provider: {
name: 'aws',
runtime: 'nodejs14.x',
},
functions: {
hello: {
handler: 'src/publisher.handler',
}
}
};

module.exports = serverlessConfiguration;
```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"lint": "eslint .",
"check-format": "prettier --check .",
"type": "tsc --noEmit",
"build": "etsc",
"build": "etsc && tsc --emitDeclarationOnly",
"watch": "nodemon",
"test": "jest",
"prepare": "husky install && npm run build"
Expand All @@ -87,7 +87,7 @@
"eslint --fix"
]
},
"types": "lib/index.d.ts",
"types": "dist/src/plugin.d.ts",
"peerDependencies": {
"serverless": "^2.36.0"
}
Expand Down
11 changes: 8 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from "path";
import { readFileSync } from "fs";
import { dump } from "js-yaml";
import { DefaultTokenResolver, Lazy, StringConcat, Tokenization } from "@aws-cdk/core";
import { FromSchema } from "json-schema-to-ts";
import type {
CommandsDefinition,
DeprecatedVariableResolver,
Expand All @@ -30,11 +31,11 @@ const CONSTRUCTS_DEFINITION = {
},
required: ["type"],
},
] as Record<string, unknown>[],
],
},
},
additionalProperties: false,
};
} as const;

/**
* Serverless plugin
Expand Down Expand Up @@ -97,7 +98,7 @@ class LiftPlugin {
}

private registerConstructsSchema() {
this.schema.patternProperties[CONSTRUCT_ID_PATTERN].allOf.push({
(this.schema.patternProperties[CONSTRUCT_ID_PATTERN].allOf as unknown as Record<string, unknown>[]).push({
oneOf: this.getAllConstructClasses().map((Construct) => {
return this.defineConstructSchema(Construct.type, Construct.schema);
}),
Expand Down Expand Up @@ -349,4 +350,8 @@ class LiftPlugin {
}
}

export type Lift = {
constructs: FromSchema<typeof CONSTRUCTS_DEFINITION>;
};

module.exports = LiftPlugin;

0 comments on commit 5ad131b

Please sign in to comment.