diff --git a/README.md b/README.md index 4aa80927d..0f7bdba66 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/helloworld-samples/function-typescript-codebundle-nodejs/README.md b/helloworld-samples/function-typescript-codebundle-nodejs/README.md new file mode 100644 index 000000000..86de1d7de --- /dev/null +++ b/helloworld-samples/function-typescript-codebundle-nodejs/README.md @@ -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) + diff --git a/helloworld-samples/function-typescript-codebundle-nodejs/main.ts b/helloworld-samples/function-typescript-codebundle-nodejs/main.ts new file mode 100644 index 000000000..3caba640a --- /dev/null +++ b/helloworld-samples/function-typescript-codebundle-nodejs/main.ts @@ -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), + }; +} diff --git a/helloworld-samples/function-typescript-codebundle-nodejs/package.json b/helloworld-samples/function-typescript-codebundle-nodejs/package.json new file mode 100644 index 000000000..bc50d5c9c --- /dev/null +++ b/helloworld-samples/function-typescript-codebundle-nodejs/package.json @@ -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" + } +} diff --git a/helloworld-samples/function-typescript-codebundle-nodejs/tsconfig.json b/helloworld-samples/function-typescript-codebundle-nodejs/tsconfig.json new file mode 100644 index 000000000..ceee3de58 --- /dev/null +++ b/helloworld-samples/function-typescript-codebundle-nodejs/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "module": "nodenext", + "target": "esnext", + } +}