Skip to content

Commit ca1029b

Browse files
committed
TypeScript Node.JS Function sample
Signed-off-by: Luke Roy <[email protected]>
1 parent adbfc25 commit ca1029b

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ is demonstrating.
164164
This example shows how to create simple inline Python function
165165
- [function-codebundle-nodejs](helloworld-samples/function-codebundle-nodejs)
166166
This example shows how to create NodeJS functions with additional modules
167+
- [function-typescript-codebundle-nodejs](helloworld-samples/function-typescript-codebundle-nodejs)
168+
This example shows how to create TypeScript functions with additional modules
167169
- [function-codebundle-python](helloworld-samples/function-codebundle-python)
168170
This example shows how to create Python functions with additional modules
169171
- [function-http-nodejs](helloworld-samples/function-http-nodejs)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# NodeJS Typescript Function with additional packages
2+
3+
A sample TypeScript function (main.ts) that uses an external npm module referenced by the package.json file.
4+
5+
Deploy the function straight to Code Engine by running the following command from the source directory
6+
7+
```bash
8+
ibmcloud ce fn create -n ts-lorem-node -runtime nodejs-22 --build-source .
9+
```
10+
11+
For more information follow the official docs -> [Including modules for a Node.js Function](https://cloud.ibm.com/docs/codeengine?topic=codeengine-fun-create-repo#function-nodejs-dep-repo)
12+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// ======================================================= //
2+
// IBM Cloud Code Engine - Functions-as-a-Service Sample //
3+
// //
4+
// main.ts (TypeScript sample) //
5+
// //
6+
// This sample code uses an external module "lorem-ipsum" //
7+
// to generate an arbitrary result message. IBM code //
8+
// engine functions code with references to external //
9+
// modules have to be deployed as code-bundles. //
10+
// //
11+
// This sample shows how an external reference is coded //
12+
// in the source file (main.ts) and how the module is //
13+
// referenced in the packages.json file //
14+
// ======================================================= //
15+
16+
/**
17+
* The `main` function is the entry-point into the function.
18+
*
19+
* A Code engine function source file can define multiple
20+
* functions, but it needs to have one dedicated 'main'
21+
* function, which will be called by the code engine runtime
22+
* on function's invocation.
23+
*
24+
* The 'main' function has one optional argument, which
25+
* carries all the parameters the function was invoked with.
26+
* But in this example the input argument is not used.
27+
*
28+
*/
29+
30+
// refernce external module
31+
import { LoremIpsum } from "lorem-ipsum";
32+
33+
interface Params {
34+
[key: string]: any;
35+
}
36+
37+
interface Response {
38+
headers: { [key: string]: string };
39+
body: string;
40+
}
41+
42+
// export the main function within this source file
43+
// as the 'main' symbol to make it known to the runtime
44+
// This also works, if the function name is not 'main'.
45+
// By exporting the function as 'main', it will be
46+
// found and invoked by the runtime.
47+
export function main(params?: Params): Response {
48+
// create a default text generator.
49+
const lorem = new LoremIpsum();
50+
// finally, build an HTML response that can be rendered
51+
// properly in a browser. To do so, we need to specify
52+
// the correct 'Content-Type' ('text/html' for HTML markup)
53+
// Alternatively, we could also use 'text/plain' or
54+
// 'application/json', depending on the text we plan to
55+
// return.
56+
return {
57+
// specify headers for the HTTP response
58+
// we only set the Content-Type in this case, to
59+
// ensure the text is properly displayed in the browser
60+
headers: { "Content-Type": "text/plain;charset=utf-8" },
61+
// use the text generator to create a response sentence
62+
// with 10 words
63+
body: lorem.generateWords(10),
64+
};
65+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "ts-func",
3+
"version": "1.0.0",
4+
"main": "main.js",
5+
"scripts": {
6+
"postinstall": "tsc"
7+
},
8+
"devDependencies": {
9+
"typescript": "^5.9.2"
10+
},
11+
"dependencies": {
12+
"lorem-ipsum": "^2.0.8"
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"module": "nodenext",
4+
"target": "esnext",
5+
}
6+
}

0 commit comments

Comments
 (0)