Skip to content

Commit 5ad131b

Browse files
Merge pull request #81 from getlift/add-type-generation
Add types definition for serverless.ts
2 parents 53d46aa + 97f82b9 commit 5ad131b

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ To eject:
131131
- 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/)
132132
- don't forget to remove from `serverless.yml` the Lift constructs you have turned into CloudFormation
133133

134+
## [Types definition for Lift constructs](docs/serverless-types.md)
135+
134136
---
135137

136138
Lift is built and maintained with love ❤️ by

docs/serverless-types.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Lift Typescript definitions
2+
3+
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).
4+
5+
Generated Typescript types for the service file from [@serverless/typescript](https://github.com/serverless/typescript) do NOT include `constructs` definition from Lift.
6+
7+
In order to cope this issue, Lift exports a type definition you can use in conjonction with the official Serverless definition:
8+
9+
_serverless.ts_
10+
```ts
11+
import type { AWS } from '@serverless/typescript';
12+
import type { Lift } from "serverless-lift";
13+
14+
const serverlessConfiguration: AWS & Lift = {
15+
service: 'myService',
16+
frameworkVersion: '2',
17+
plugins: ['serverless-lift'],
18+
constructs: {
19+
avatars: {
20+
type: 'storage',
21+
},
22+
},
23+
provider: {
24+
name: 'aws',
25+
runtime: 'nodejs14.x',
26+
},
27+
functions: {
28+
hello: {
29+
handler: 'src/publisher.handler',
30+
}
31+
}
32+
};
33+
34+
module.exports = serverlessConfiguration;
35+
```

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"lint": "eslint .",
7575
"check-format": "prettier --check .",
7676
"type": "tsc --noEmit",
77-
"build": "etsc",
77+
"build": "etsc && tsc --emitDeclarationOnly",
7878
"watch": "nodemon",
7979
"test": "jest",
8080
"prepare": "husky install && npm run build"
@@ -87,7 +87,7 @@
8787
"eslint --fix"
8888
]
8989
},
90-
"types": "lib/index.d.ts",
90+
"types": "dist/src/plugin.d.ts",
9191
"peerDependencies": {
9292
"serverless": "^2.36.0"
9393
}

src/plugin.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from "path";
55
import { readFileSync } from "fs";
66
import { dump } from "js-yaml";
77
import { DefaultTokenResolver, Lazy, StringConcat, Tokenization } from "@aws-cdk/core";
8+
import { FromSchema } from "json-schema-to-ts";
89
import type {
910
CommandsDefinition,
1011
DeprecatedVariableResolver,
@@ -30,11 +31,11 @@ const CONSTRUCTS_DEFINITION = {
3031
},
3132
required: ["type"],
3233
},
33-
] as Record<string, unknown>[],
34+
],
3435
},
3536
},
3637
additionalProperties: false,
37-
};
38+
} as const;
3839

3940
/**
4041
* Serverless plugin
@@ -97,7 +98,7 @@ class LiftPlugin {
9798
}
9899

99100
private registerConstructsSchema() {
100-
this.schema.patternProperties[CONSTRUCT_ID_PATTERN].allOf.push({
101+
(this.schema.patternProperties[CONSTRUCT_ID_PATTERN].allOf as unknown as Record<string, unknown>[]).push({
101102
oneOf: this.getAllConstructClasses().map((Construct) => {
102103
return this.defineConstructSchema(Construct.type, Construct.schema);
103104
}),
@@ -349,4 +350,8 @@ class LiftPlugin {
349350
}
350351
}
351352

353+
export type Lift = {
354+
constructs: FromSchema<typeof CONSTRUCTS_DEFINITION>;
355+
};
356+
352357
module.exports = LiftPlugin;

0 commit comments

Comments
 (0)