Skip to content

Commit de08fc4

Browse files
Handle hyphens in function names (#12)
Co-authored-by: Roni Frantchi <roni-frantchi@users.noreply.github.com>
1 parent 956e518 commit de08fc4

File tree

7 files changed

+145
-4
lines changed

7 files changed

+145
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ The `requestModels` property allows you to define models for the HTTP Request of
330330
These will be autogenerated for you from TypeScript **by convention**.
331331
The plugin will autogenerate a request model for every `put`, `patch` or `post` method based on the TypeScript type found by convention.
332332
The look strategy will be by:
333-
`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body`
333+
`${serverless.custom.documentation.apiNamespace}.${upper-case-name-of-function}.Request.Body`. Note that any underscores (`_`) or hyphens (`-`) in function names will be excluded in the resolved type (e.g. the Serverless function `my_get-function` will resolve to `MyGetFunction` in Typescript)
334334

335335
For example, the following function:
336336
```yaml

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-typescript",
3-
"version": "1.0.9",
3+
"version": "1.0.10",
44
"description": "An extension of @conqa/serverless-openapi-documentation that also generates your OpenAPI models from TypeScript",
55
"main": "dist/index.js",
66
"scripts": {

src/serverless-openapi-typescript.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type Serverless from "serverless";
22
import fs from "fs";
33
import yaml from "js-yaml";
44
import {SchemaGenerator, createGenerator} from "ts-json-schema-generator";
5-
import {upperFirst, mergeWith, set, isArray, get, isEmpty } from "lodash" ;
5+
import {upperFirst, camelCase, mergeWith, set, isArray, get, isEmpty } from "lodash" ;
66
import {ApiGatewayEvent} from "serverless/plugins/aws/package/compile/events/apiGateway/lib/validate";
77

88
interface Options {
@@ -138,7 +138,7 @@ export default class ServerlessOpenapiTypeScript {
138138
}
139139

140140
setModels(httpEvent, functionName) {
141-
const definitionPrefix = `${this.serverless.service.custom.documentation.apiNamespace}.${upperFirst(functionName)}`;
141+
const definitionPrefix = `${this.serverless.service.custom.documentation.apiNamespace}.${upperFirst(camelCase(functionName))}`;
142142
const method = httpEvent.method.toLowerCase();
143143
switch (method) {
144144
case 'delete':
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
openapi: 3.1.0
2+
components:
3+
schemas:
4+
ProjectApi.CreateFunc.Request.Body:
5+
type: 'null'
6+
ProjectApi.CreateFunc.Response:
7+
type: 'null'
8+
ProjectApi.GetFunc.Response:
9+
type: 'null'
10+
info:
11+
title: Project
12+
description: DummyDescription
13+
version: 3a5b57df-54e7-4dd2-9423-d4674809c816
14+
paths:
15+
/create:
16+
post:
17+
operationId: create-func
18+
summary: Create Function
19+
description: |
20+
Create Function1
21+
Create Function2
22+
Create Function3
23+
requestBody:
24+
content:
25+
application/json:
26+
schema:
27+
$ref: '#/components/schemas/ProjectApi.CreateFunc.Request.Body'
28+
description: ''
29+
parameters: []
30+
responses:
31+
'200':
32+
description: ''
33+
content:
34+
application/json:
35+
schema:
36+
$ref: '#/components/schemas/ProjectApi.CreateFunc.Response'
37+
tags:
38+
- FooBarTitle
39+
/delete:
40+
delete:
41+
operationId: delete-all-func
42+
summary: Delete Function
43+
description: Delete
44+
parameters: []
45+
responses:
46+
'204':
47+
description: Status 204 Response
48+
content: {}
49+
tags:
50+
- Project
51+
/get:
52+
get:
53+
operationId: get_func
54+
summary: Get Function
55+
parameters: []
56+
responses:
57+
'200':
58+
description: ''
59+
content:
60+
application/json:
61+
schema:
62+
$ref: '#/components/schemas/ProjectApi.GetFunc.Response'
63+
tags:
64+
- BazTitle
65+
tags:
66+
- name: Project
67+
description: DummyDescription
68+
- name: FooBarTitle
69+
description: FooBarDescription
70+
- name: BazTitle
71+
description: BazDescription
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export namespace ProjectApi {
2+
3+
export namespace CreateFunc {
4+
export namespace Request {
5+
export type Body = void
6+
}
7+
8+
export type Response = void
9+
}
10+
11+
export namespace GetFunc {
12+
export type Response = void
13+
}
14+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
service: serverless-openapi-typescript-demo
2+
provider:
3+
name: aws
4+
5+
plugins:
6+
- ../node_modules/@conqa/serverless-openapi-documentation
7+
- ../src/index
8+
9+
custom:
10+
documentation:
11+
title: 'Project'
12+
description: DummyDescription
13+
apiNamespace: ProjectApi
14+
tags:
15+
- name: FooBarTitle
16+
description: FooBarDescription
17+
- name: BazTitle
18+
description: BazDescription
19+
20+
21+
functions:
22+
create-func:
23+
handler: handler.create
24+
events:
25+
- http:
26+
documentation:
27+
summary: "Create Function"
28+
tag: FooBarTitle
29+
description: |
30+
Create Function1
31+
Create Function2
32+
Create Function3
33+
path: create
34+
method: post
35+
36+
delete-all-func:
37+
handler: handler.delete
38+
events:
39+
- http:
40+
documentation:
41+
summary: "Delete Function"
42+
description: "Delete"
43+
path: delete
44+
method: delete
45+
46+
get_func:
47+
handler: handler.update
48+
events:
49+
- http:
50+
documentation:
51+
summary: "Get Function"
52+
tag: BazTitle
53+
path: get
54+
method: get
55+

test/serverless-openapi-typescript.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ describe('ServerlessOpenapiTypeScript', () => {
1414
describe.each`
1515
testCase | projectName
1616
${'Custom Tags'} | ${'custom-tags'}
17+
${'Hyphenated Functions'} | ${'hyphenated-functions'}
1718
${'Full Project'} | ${'full'}
1819
`('when using $testCase', ({projectName}) => {
1920

0 commit comments

Comments
 (0)