Skip to content

Commit 7542541

Browse files
committed
Typescript Hello example with tests. (#156)
1 parent 8ab184a commit 7542541

File tree

11 files changed

+262
-0
lines changed

11 files changed

+262
-0
lines changed

hello-typescript/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
lib/src/**/*.js
2+
lib/src/**/*d.ts
3+
lib/test/**/*d.ts
4+
lib/test/**/*.js
5+
lib/coverage
6+
packages/hello-ts/hello/index.js

hello-typescript/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
## Nimbella Serverless Cloud
2+
3+
Thank you for trying out Nimbella to deploy a TypeScript serverless project.
4+
5+
### Hello World
6+
We created a starter project on [GitHub](https://github.com/nimbella/demo-projects/tree/master/hello-typescript). The project consists of the following files:
7+
8+
- [`lib/src/hello/Hello.ts`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/lib/src/hello/Hello.ts): a sample function which implements a greeting API.
9+
- [`lib/test/hello/Hello.test.ts`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/lib/test/hello/Hello.test.ts): a file containing unit tests for the sample function, implemented using [Jest](https://jestjs.io/).
10+
11+
- [`lib/build.sh`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/lib/build.sh): a build script which runs the TypeScript compiler to generate JavaScript. The compiled output is webpacked, and the result is used to create the serverless API. The webpack configuration is located in [`lib/src/hello/webpack.config.js`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/lib/src/hello/webpack.config.js). The TypeScript compiler configuration can be found in [`lib/tsconfig.json`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/lib/tsconfig.json).
12+
13+
- [`packages/hello-ts/hello`](https://github.com/nimbella/demo-projects/tree/master/hello-typescript/packages/hello-ts/hello): a directory which will contain the implementation of a serverless API `/api/hello-ts/hello`.
14+
- [`build.sh`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/packages/hello-ts/hello/build.sh): a shell script for Mac and GNU/Linux platforms, to build and package the functions for deployment to the cloud.
15+
- [`package.json`](https://github.com/nimbella/demo-projects/blob/master/hello-typescript/packages/hello-ts/hello/package.json): this is a Node.js pacakges file to install dependencies. This sample does not require dependencies.
16+
17+
### Install the Nimbella CLI
18+
19+
You will need the Nimbella CLI to get started.
20+
Please download the CLI specific for your platform from [https://nimbella-apigcp.nimbella.io/login.html?token=_#cli](https://nimbella-apigcp.nimbella.io/login.html?token=_#cli).
21+
22+
Once you install the CLI, you will need to run:
23+
24+
```
25+
nim login
26+
```
27+
28+
If you do not yet have an account, this will be your opportunity to sign up (it's free and no upfront information is needed except your e-mail or GitHub id).
29+
30+
### Deploy your first project
31+
32+
You are now ready to deploy and run the starter project in three steps.
33+
34+
#### 1. Clone the demo project from GitHub
35+
36+
```
37+
git clone git@github.com:nimbella/demo-projects.git
38+
```
39+
40+
#### 2. Deploy the project
41+
```
42+
nim project deploy demo-projects/hello-typescript
43+
```
44+
45+
You will see output similar to:
46+
47+
```
48+
Deploying project '/path/to/demo-projects/hello-typescript'
49+
to namespace 'example-namespace'
50+
on host 'https://apigcp.nimbella.io'
51+
Started running ./build.sh in nimbella/demo-projects/hello-typescript/hello-ts/hello
52+
Still running ./build.sh in nimbella/demo-projects/hello-typescript/hello-ts/hello
53+
...
54+
Finished running ./build.sh in nimbella/demo-projects/hello-typescript/hello-ts/hello
55+
Deployment status recorded in 'hello-typescript/.nimbella'
56+
57+
Deployed actions ('nim action get <actionName> --url' for URL):
58+
- hello-ts/hello
59+
```
60+
61+
#### 3. Run your serverless API
62+
63+
```
64+
nim action invoke hello-ts/hello --param name World
65+
```
66+
67+
This will show the following result:
68+
69+
```
70+
{
71+
"body": "Hello, World!"
72+
}
73+
```
74+
75+
The API you deployed can be used from the browser or via `curl`, Postman, or your favorite API testing tool.
76+
To get the URL for the API run the following command.
77+
78+
```
79+
nim action get hello-ts/hello --url
80+
```
81+
82+
You may pass parameters either as query parameters or as JSON content. Here are two examples using `curl`.
83+
84+
```
85+
> curl `nim action get hello-ts/hello --url`?name=typescript
86+
Hello, typescript!
87+
```
88+
89+
```
90+
> curl `nim action get hello-ts/hello --url` \
91+
-X POST \
92+
-H 'content-type: application/json' \
93+
-d '{"name":"again"}'
94+
Hello, again!
95+
```
96+
97+
### Running the tests
98+
99+
This example includes unit tests in the `lib/test` directory. You can run the tests as shown below.
100+
101+
```
102+
cd lib
103+
npm install
104+
npm run test
105+
```
106+
107+
### Support and Feature Requests
108+
109+
We welcome your feedback and thoughts on what features will be helpful to make your serverless journey a success.
110+
111+
- For general bugs and enhancements, we suggest opening [an issue on GitHub](https://github.com/nimbella/nimbella-cli/issues/new).
112+
- For quick questions, you can reach us in [Slack](nimbella-community.slack.com).

hello-typescript/lib/.jestrc.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"collectCoverage": true,
3+
"coverageDirectory": "coverage",
4+
"collectCoverageFrom": [
5+
"src/**/*.ts"
6+
],
7+
"transform": {
8+
"^.+\\.(ts|tsx)$": "ts-jest"
9+
},
10+
"testPathIgnorePatterns": [
11+
"/node_modules/",
12+
"(/__tests__/.*|(\\.|/)(test|spec))\\.js$"
13+
],
14+
"coverageThreshold": {
15+
"global": {
16+
"branches": 0,
17+
"lines": 0,
18+
"statements": 0
19+
}
20+
},
21+
"testEnvironment": "node"
22+
}

hello-typescript/lib/build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SELFDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
TSC=tsc
7+
8+
if type -P "${TSC}" &>/dev/null; then
9+
echo "tsc is in path, will use global install"
10+
else
11+
echo "tsc is not in path, will use local install"
12+
TSC=${SELFDIR}/node_modules/.bin/tsc
13+
fi
14+
15+
(cd "${SELFDIR}" && npm install && "${TSC}")
16+
(cd "${SELFDIR}/src/hello" && npx webpack)

hello-typescript/lib/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "hello-typescript",
3+
"version": "1.0.0",
4+
"description": "Example project using TypeScript",
5+
"scripts": {
6+
"build": "tsc",
7+
"test": "jest --config=./.jestrc.json ./test",
8+
"clean": "find src -type f \\( -iname '*.d.ts' -or -iname '*.js' ! -iname 'webpack.*' \\) | xargs rm",
9+
"clear": "jest --clearCache"
10+
},
11+
"devDependencies": {
12+
"@types/jest": "^24.9.0",
13+
"jest": "^24.9.0",
14+
"ts-jest": "^24.3.0",
15+
"tslib": "^1.11.1",
16+
"typescript": "^3.7.3",
17+
"ts-loader": "^6.2.2",
18+
"webpack": "^4.42.1",
19+
"webpack-cli": "^3.3.11"
20+
}
21+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function main(args: {}): {} {
2+
let name: string = args['name'] || 'stranger'
3+
let greeting: string = 'Hello, ' + name + '!'
4+
console.log(greeting)
5+
return { body: greeting }
6+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path')
2+
const actionDir = '../../../packages/hello-ts/hello/'
3+
4+
module.exports = {
5+
entry: './Hello.ts',
6+
mode: 'production',
7+
target: 'node',
8+
node: false,
9+
module: {
10+
rules: [
11+
{
12+
test: /\.tsx?$/,
13+
use: 'ts-loader',
14+
exclude: /node_modules/,
15+
},
16+
],
17+
},
18+
resolve: {
19+
extensions: [ '.tsx', '.ts', '.js' ],
20+
},
21+
externals: function(context, request, callback) {
22+
if (/^[^.]/.test(request)){
23+
return callback(null, 'commonjs2 ' + request)
24+
}
25+
callback()
26+
},
27+
output: {
28+
filename: 'index.js',
29+
path: path.resolve(actionDir),
30+
libraryTarget: 'commonjs-module'
31+
},
32+
optimization: {
33+
minimize: false
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { main } from '../../src/hello/Hello'
2+
3+
describe('hello', () => {
4+
it('should respond with standard greeting', async () => {
5+
expect.assertions(1)
6+
const res = await main({})
7+
expect(res).toEqual({'body': 'Hello, stranger!'})
8+
})
9+
10+
it('should respond with name in greeting', async () => {
11+
expect.assertions(1)
12+
const res = await main({name: 'jest'})
13+
expect(res).toEqual({'body': 'Hello, jest!'})
14+
})
15+
})
16+

hello-typescript/lib/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"importHelpers": true,
5+
"module": "commonjs",
6+
"target": "es2017",
7+
"rootDir": "./"
8+
},
9+
"include": [
10+
"src/**/*.ts",
11+
"test/**/*.ts"
12+
]
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SELFDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
6+
7+
../../../lib/build.sh
8+
npm install --production

0 commit comments

Comments
 (0)