Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ is demonstrating.
This example shows how to create simple inline Python function
- [function-codebundle-nodejs](helloworld-samples/function-codebundle-nodejs)
This example shows how to create NodeJS functions with additional modules
- [function-typescript-codebundle-nodejs](helloworld-samples/function-typescript-codebundle-nodejs)
This example shows how to create TypeScript functions with additional modules
- [function-codebundle-python](helloworld-samples/function-codebundle-python)
This example shows how to create Python functions with additional modules
- [function-http-nodejs](helloworld-samples/function-http-nodejs)
Expand Down
12 changes: 12 additions & 0 deletions helloworld-samples/function-typescript-codebundle-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# NodeJS Typescript Function with additional packages

A sample TypeScript function (main.ts) that uses an external npm module referenced by the package.json file.

Deploy the function straight to Code Engine by running the following command from the source directory

```bash
ibmcloud ce fn create -n ts-lorem-node -runtime nodejs-22 --build-source .
```

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)

65 changes: 65 additions & 0 deletions helloworld-samples/function-typescript-codebundle-nodejs/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// ======================================================= //
// IBM Cloud Code Engine - Functions-as-a-Service Sample //
// //
// main.ts (TypeScript sample) //
// //
// This sample code uses an external module "lorem-ipsum" //
// to generate an arbitrary result message. IBM code //
// engine functions code with references to external //
// modules have to be deployed as code-bundles. //
// //
// This sample shows how an external reference is coded //
// in the source file (main.ts) and how the module is //
// referenced in the packages.json file //
// ======================================================= //

/**
* The `main` function is the entry-point into the function.
*
* A Code engine function source file can define multiple
* functions, but it needs to have one dedicated 'main'
* function, which will be called by the code engine runtime
* on function's invocation.
*
* The 'main' function has one optional argument, which
* carries all the parameters the function was invoked with.
* But in this example the input argument is not used.
*
*/

// refernce external module
import { LoremIpsum } from "lorem-ipsum";

interface Params {
[key: string]: any;
}

interface Response {
headers: { [key: string]: string };
body: string;
}

// export the main function within this source file
// as the 'main' symbol to make it known to the runtime
// This also works, if the function name is not 'main'.
// By exporting the function as 'main', it will be
// found and invoked by the runtime.
export function main(params?: Params): Response {
// create a default text generator.
const lorem = new LoremIpsum();
// finally, build an HTML response that can be rendered
// properly in a browser. To do so, we need to specify
// the correct 'Content-Type' ('text/html' for HTML markup)
// Alternatively, we could also use 'text/plain' or
// 'application/json', depending on the text we plan to
// return.
return {
// specify headers for the HTTP response
// we only set the Content-Type in this case, to
// ensure the text is properly displayed in the browser
headers: { "Content-Type": "text/plain;charset=utf-8" },
// use the text generator to create a response sentence
// with 10 words
body: lorem.generateWords(10),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "ts-func",
"version": "1.0.0",
"main": "main.js",
"scripts": {
"postinstall": "tsc"
},
"devDependencies": {
"typescript": "^5.9.2"
},
"dependencies": {
"lorem-ipsum": "^2.0.8"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"module": "nodenext",
"target": "esnext",
}
}