Skip to content

Commit d1fbd4a

Browse files
Merge pull request #21 from DataDog/darcy.rayner/add-ts-support
Add typescript support
2 parents ce4bb23 + 643ac40 commit d1fbd4a

File tree

5 files changed

+97
-12
lines changed

5 files changed

+97
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-plugin-datadog",
3-
"version": "0.12.0",
3+
"version": "0.13.0",
44
"description": "Serverless plugin to automatically instrument python and node functions with datadog tracing",
55
"main": "dist/index.js",
66
"repository": "https://github.com/DataDog/serverless-plugin-datadog",

src/layer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Service from "serverless/classes/Service";
1111

1212
export enum RuntimeType {
1313
NODE,
14+
NODE_TS,
1415
PYTHON,
1516
UNSUPPORTED,
1617
}

src/templates/node-ts-template.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed
3+
* under the Apache License Version 2.0.
4+
*
5+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
6+
* Copyright 2019 Datadog, Inc.
7+
*/
8+
9+
export function typescriptTemplate(filePath: string, method: string) {
10+
return `/* tslint:disable */
11+
/* eslint-disable */
12+
const { datadog } = require("datadog-lambda-js") as any;
13+
import * as original from "../${filePath}";
14+
export const ${method} = datadog(original.${method});`;
15+
}

src/wrapper.spec.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ describe("getWrapperText", () => {
2424
},
2525
});
2626
expect(wrapperText).toMatchInlineSnapshot(`
27-
Object {
28-
"method": "myhandler",
29-
"text": "from datadog_lambda.wrapper import datadog_lambda_wrapper
30-
from mydir.func import myhandler as myhandler_impl
31-
myhandler = datadog_lambda_wrapper(myhandler_impl)",
32-
}
33-
`);
27+
Object {
28+
"method": "myhandler",
29+
"text": "from datadog_lambda.wrapper import datadog_lambda_wrapper
30+
from mydir.func import myhandler as myhandler_impl
31+
myhandler = datadog_lambda_wrapper(myhandler_impl)",
32+
}
33+
`);
3434
});
3535
it("renders the node template correctly", () => {
3636
const wrapperText = getWrapperText({
@@ -42,12 +42,33 @@ describe("getWrapperText", () => {
4242
handler: "my.myhandler",
4343
},
4444
});
45+
expect(wrapperText).toMatchInlineSnapshot(`
46+
Object {
47+
"method": "myhandler",
48+
"text": "const { datadog } = require(\\"datadog-lambda-js\\");
49+
const original = require(\\"../my\\");
50+
module.exports.myhandler = datadog(original.myhandler);",
51+
}
52+
`);
53+
});
54+
it("renders the node ts template correctly", () => {
55+
const wrapperText = getWrapperText({
56+
name: "my-lambda",
57+
type: RuntimeType.NODE_TS,
58+
handler: {
59+
name: "",
60+
package: {} as any,
61+
handler: "my.myhandler",
62+
},
63+
});
4564
expect(wrapperText).toMatchInlineSnapshot(`
4665
Object {
4766
"method": "myhandler",
48-
"text": "const { datadog } = require(\\"datadog-lambda-js\\");
49-
const original = require(\\"../my\\");
50-
module.exports.myhandler = datadog(original.myhandler);",
67+
"text": "/* tslint:disable */
68+
/* eslint-disable */
69+
const { datadog } = require(\\"datadog-lambda-js\\") as any;
70+
import * as original from \\"../my\\";
71+
export const myhandler = datadog(original.myhandler);",
5172
}
5273
`);
5374
});
@@ -182,4 +203,23 @@ describe("writeHandlers", () => {
182203
]);
183204
expect(fs.existsSync(`${datadogDirectory}/my-lambda.py`)).toBeTruthy();
184205
});
206+
it("uses the typescript template when the handler file is ts", async () => {
207+
mock({
208+
"mylambda.ts": "",
209+
});
210+
const service = {} as any;
211+
await writeHandlers(service, [
212+
{
213+
name: "my-lambda",
214+
type: RuntimeType.NODE,
215+
handler: {
216+
name: "my-lambda",
217+
218+
package: {} as any,
219+
handler: "mylambda.myhandler",
220+
},
221+
},
222+
]);
223+
expect(fs.existsSync(`${datadogDirectory}/my-lambda.ts`)).toBeTruthy();
224+
});
185225
});

src/wrapper.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import Service from "serverless/classes/Service";
1212
import util from "util";
1313
import { HandlerInfo, RuntimeType } from "./layer";
1414
import { nodeTemplate } from "./templates/node-js-template";
15+
import { typescriptTemplate } from "./templates/node-ts-template";
1516
import { pythonTemplate } from "./templates/python-template";
1617
import { removeDirectory } from "./util";
1718

@@ -22,6 +23,18 @@ export async function writeHandlers(service: Service, handlers: HandlerInfo[]) {
2223
await util.promisify(fs.mkdir)(datadogDirectory);
2324

2425
const promises = handlers.map(async (handlerInfo) => {
26+
const handlerPath = getHandlerPath(handlerInfo);
27+
if (handlerPath === undefined) {
28+
return;
29+
}
30+
31+
if (handlerInfo.type === RuntimeType.NODE && fs.existsSync(`./${handlerPath.filename}.ts`)) {
32+
handlerInfo = {
33+
...handlerInfo,
34+
type: RuntimeType.NODE_TS,
35+
};
36+
}
37+
2538
const result = getWrapperText(handlerInfo);
2639
if (result === undefined) {
2740
return;
@@ -61,14 +74,17 @@ export function getWrapperText(handlerInfo: HandlerInfo) {
6174
switch (handlerInfo.type) {
6275
case RuntimeType.NODE:
6376
return { text: nodeTemplate(filename, method), method };
77+
case RuntimeType.NODE_TS:
78+
return { text: typescriptTemplate(filename, method), method };
6479
case RuntimeType.PYTHON:
6580
return { text: pythonTemplate(filename, method), method };
6681
}
6782
}
6883

6984
export async function writeWrapperFunction(handlerInfo: HandlerInfo, wrapperText: string) {
70-
const extension = handlerInfo.type === RuntimeType.PYTHON ? "py" : "js";
85+
const extension = getHandlerExtension(handlerInfo.type);
7186
const filename = `${handlerInfo.name}.${extension}`;
87+
7288
const pathname = path.join(datadogDirectory, filename);
7389
await util.promisify(fs.writeFile)(pathname, wrapperText);
7490
return pathname;
@@ -95,3 +111,16 @@ export async function addToExclusionList(service: any, files: string[]) {
95111
}
96112
pack.include.push(...files);
97113
}
114+
115+
function getHandlerExtension(type: RuntimeType) {
116+
switch (type) {
117+
case RuntimeType.NODE:
118+
return "js";
119+
case RuntimeType.NODE_TS:
120+
return "ts";
121+
case RuntimeType.PYTHON:
122+
return "py";
123+
case RuntimeType.UNSUPPORTED:
124+
return "";
125+
}
126+
}

0 commit comments

Comments
 (0)