Skip to content

Commit

Permalink
Add main package for entry of nammatham package (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize authored Jan 21, 2024
2 parents 0259e0e + 72fe329 commit 7d78647
Show file tree
Hide file tree
Showing 27 changed files with 262 additions and 91 deletions.
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.eslintrc.js
examples
examples
scripts
4 changes: 1 addition & 3 deletions examples/azure-functions-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.8",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions-minimal/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { AzureFunctionsAdapter } from "@nammatham/azure-functions";
import { initNammatham } from "@nammatham/core";
import { expressPlugin } from "@nammatham/express";
import { AzureFunctionsAdapter, initNammatham, expressPlugin } from "nammatham";

const n = initNammatham.create(new AzureFunctionsAdapter());
const func = n.func;
Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions-timer-trigger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.8",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-functions-timer-trigger/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expressPlugin } from '@nammatham/express';
import { expressPlugin } from 'nammatham';
import simpleTimer from './functions/simple-timer';
import { app } from './nammatham';

Expand Down
3 changes: 1 addition & 2 deletions examples/azure-functions-timer-trigger/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { initNammatham, AzureFunctionsAdapter } from 'nammatham';

const n = initNammatham.create(new AzureFunctionsAdapter());
n.func;
Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions-with-inversify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
"dependencies": {
"@azure/functions": "^4.1.0",
"@di-extra/inversify": "^0.2.0",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.8",
"inversify": "^6.0.2",
"reflect-metadata": "^0.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-functions-with-inversify/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import { expressPlugin } from '@nammatham/express';
import { expressPlugin } from 'nammatham';
import hello from './functions/hello';
import { app } from './nammatham';

Expand Down
3 changes: 1 addition & 2 deletions examples/azure-functions-with-inversify/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { initNammatham, AzureFunctionsAdapter } from 'nammatham';

const n = initNammatham.create(new AzureFunctionsAdapter());
n.func;
Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions-with-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8"
"nammatham": "2.0.0-alpha.8"
},
"devDependencies": {
"cross-env": "^7.0.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-functions-with-test/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expressPlugin } from '@nammatham/express';
import { expressPlugin } from 'nammatham';
import hello from './functions/hello';
import { app } from './nammatham';

Expand Down
4 changes: 2 additions & 2 deletions examples/azure-functions-with-test/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { initNammatham } from 'nammatham';
import { AzureFunctionsAdapter } from 'nammatham';

const n = initNammatham.create(new AzureFunctionsAdapter());

Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions-with-trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.8",
"@nammatham/trpc-azure-functions": "2.0.0-alpha.8",
"@trpc/client": "^10.45.0",
"@trpc/server": "^10.45.0",
Expand Down
3 changes: 1 addition & 2 deletions examples/azure-functions-with-trpc/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { initNammatham, AzureFunctionsAdapter } from 'nammatham';

const n = initNammatham.create(new AzureFunctionsAdapter());

Expand Down
4 changes: 1 addition & 3 deletions examples/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.8",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-functions/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expressPlugin } from '@nammatham/express';
import { expressPlugin } from 'nammatham';
import blob from './functions/blob';
import hello from './functions/hello';
import { app } from './nammatham';
Expand Down
3 changes: 1 addition & 2 deletions examples/azure-functions/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { initNammatham } from '@nammatham/core';
import { AzureFunctionsAdapter } from '@nammatham/azure-functions';
import { initNammatham, AzureFunctionsAdapter } from 'nammatham';

const n = initNammatham.create(new AzureFunctionsAdapter());
n.func;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"test": "vitest",
"test:coverage": "vitest run --coverage",
"dev": "nx run @nammatham/core:build && nx run-many -t dev --projects=@nammatham/* --parallel=5",
"dev": "nx run @nammatham/core:build && nx run-many -t dev --projects nammatham @nammatham/* --parallel=5",
"pre-local": "tsx ./scripts/pre-local.ts",
"post-local": "tsx ./scripts/post-local.ts",
"release": "run-s build releaseOnly",
Expand Down
2 changes: 1 addition & 1 deletion packages/express/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface NammathamAppRequestOption extends NammathamHttpHandlerOption {
res: express.Response;
}

export function trimSlash(str: string) {
function trimSlash(str: string) {
return str.replace(/^\/|\/$/g, '');
}

Expand Down
134 changes: 134 additions & 0 deletions packages/main/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<p align="center">
<a href="http://thadaw.com/" target="blank"><img src="https://i.ibb.co/QmTh7x4/Nammatham-Logo-v2.png" width="120" alt="Nammatham Logo" /></a>
</p>

<p align="center">
Type-safe Serverless Library for Azure Functions and friends
</p>

<p align="center"><a href="https://www.npmjs.com/package/@nammatham/core"><img src="https://img.shields.io/npm/v/@nammatham/core" alt="npm version"></a> <a href="https://www.npmjs.com/package/@nammatham/core"><img src="https://img.shields.io/npm/dt/@nammatham/core" alt="npm download"></a></p>


> 🚧 Alpha Stage: Internal Use Only 🚧
>
> Please note that Nammatham v2 is currently in its Alpha stage and is intended for internal use only. As we actively develop and refine the platform, be aware that the API may undergo frequent changes.
>
> Note: [Nammatham v1](https://www.npmjs.com/package/nammatham) is currently in maintenance mode. no new features are actively being developed
| Version | Status | Azure Functions Node.js | Branch | Build Status |
| ------- | ----------- | ----------------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| v1.x | Maintenance | v3.x | [v1.x][v1.x] | [![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/mildronize/nammatham/branch/main/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/mildronize/nammatham) |
| v2.x | Alpha | v4.x | [main][main] | [Tracking v2 Roadmap](https://github.com/thaitype/nammatham/issues?q=is%3Aissue+is%3Aopen+label%3Av2-blocker) |

[v1.x]: https://github.com/thaitype/nammatham/tree/v1.x
[main]: https://github.com/thaitype/nammatham/tree/main


## Description
Nammatham (นามธรรม in Thai, pronounced `/naam ma tham/`, means **abstract** in Thai) is Azure Function Nodejs.

## Getting Started for Azure Functions

You can see [examples](examples) or follow the minimal app getting started below:

```typescript
import { AzureFunctionsAdapter } from "@nammatham/azure-functions";
import { initNammatham } from "@nammatham/core";
import { expressPlugin } from "@nammatham/express";

const n = initNammatham.create(new AzureFunctionsAdapter());
const func = n.func;
const app = n.app;

const helloFunction = func
.httpGet('hello', {
route: 'hello-world',
})
.handler(async ({trigger, context}) => {
context.log('HTTP trigger function processed a request.');
context.debug(`Http function processed request for url "${trigger.url}"`);
const name = trigger.query.get('name') || (await trigger.text()) || 'world';
return { body: `Hello, ${name}!` };
});

app.addFunctions(helloFunction);
app.register(expressPlugin());
app.start();
```

Then edit `package.json` like this;

```json
{
"main": "dist/src/main.js",
"scripts": {
"dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts",
"start": "tsc && func start"
}
}
```

Run Dev Server on locally, (For dev server use `tsx watch` for reloading run dev server using `express` )

```
npm run dev
```

Run Azure Functions on locally (Using Official Azure Functions Node.js)

```
npm start
```


## Nammatham Packages

- [core][@nammatham/core], Nammatham Core package for initializing Nammatham App

### Available Adatpers

- [azure-functions][@nammatham/azure-functions], Azure Functions Adapter for Nammatham

### Available Plugins

- [express][@nammatham/express], Express Plugin for run server. Nammatham itself doesn't contain any server, enabling this plugin to provide better DX than the original server e.g. Azure Functions Runtime
- [trpc-azure-functions][@nammatham/trpc-azure-functions], provide [tRPC](https://trpc.io/) Plugin for Azure Functions, inclduing [express][@nammatham/express] server for local testing.

[@nammatham/core]: packages/core
[@nammatham/azure-functions]: packages/azure-functions
[@nammatham/express]: packages/express
[@nammatham/trpc-azure-functions]: packages/trpc-azure-functions


## Talks
Empowering TypeScript on Azure Functions with Nammatham, Azure Open Source Day @ Microsoft Thailand, 25 Mar 2023
[![](docs/imgs/azure-open-source-day-2023.png)](https://www.youtube.com/watch?v=n6B4-5Lt2h0) (Thai speech, subtitle will added later)
- Slides: https://docs.google.com/presentation/d/1WUIXaUxXaiixZ2bgGCfx-f4Gdrmjl4RfbwKaEfAC6t4/edit?usp=sharing


<!-- ## What's different with Azure Functions v4 (Official Library) -->

## Local Dev Setup

```bash
# Install dependencies
pnpm install
# Before dev (Update workspace to local dependencies)
pnpm pre-local && pnpm install
# While dev
pnpm dev
# After dev before submitting PRs (Update workspace to actual dependencies), `pnpm install` for making sure lockfile is correct.
pnpm post-local && pnpm install
# Release package
pnpm release
```

## Inspiration
- [Azure Functions .NET](https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-csharp?tabs=azure-cli%2Cin-process)
- [inversify-express-utils](https://github.com/inversify/inversify-express-utils) - We use inversify as a Dependency Injection Tool.
- [Nestjs](https://nestjs.com/)
- [typestack/routing-controllers](https://github.com/typestack/routing-controllers)
- [azure-middleware](https://github.com/emanuelcasco/azure-middleware) - Azure Functions Middleware Libray

## Author
- Thada Wangthammang, Software Engineer, Thailand
61 changes: 61 additions & 0 deletions packages/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "nammatham",
"version": "2.0.0-alpha.8",
"description": "Type-safe Serverless Library for Azure Functions and friends",
"main": "dist/main.js",
"types": "dist/main.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/main.d.ts",
"import": "./dist/main.mjs",
"default": "./dist/main.js"
},
"./express": {
"types": "./dist/express.d.ts",
"import": "./dist/express.mjs",
"default": "./dist/express.js"
},
"./core": {
"types": "./dist/core.d.ts",
"import": "./dist/core.mjs",
"default": "./dist/core.js"
},
"./azure-functions": {
"types": "./dist/azure-functions.d.ts",
"import": "./dist/azure-functions.mjs",
"default": "./dist/azure-functions.js"
}
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"prepublishOnly": "npm run build",
"build:main": "tsup src/main.ts --dts --format esm,cjs",
"build:express": "tsup src/express.ts --dts --format esm,cjs",
"build:core": "tsup src/core.ts --dts --format esm,cjs",
"build:azure-functions": "tsup src/azure-functions.ts --dts --format esm,cjs",
"build": "run-p build:*",
"format": "prettier -w src",
"lint": "tsc --noEmit && eslint ./src && prettier -c src",
"lint:fix": "eslint --fix ./src && prettier -w src",
"dev": "nodemon --watch src --ext ts --exec 'npm run build'"
},
"keywords": [
"azure-functions",
"azure"
],
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@nammatham/core": "2.0.0-alpha.8",
"@nammatham/azure-functions": "2.0.0-alpha.8",
"@nammatham/express": "2.0.0-alpha.8"
},
"repository": {
"type": "git",
"url": "https://github.com/thaitype/nammatham.git"
},
"publishConfig": {
"registry": "https://registry.npmjs.org/"
}
}
1 change: 1 addition & 0 deletions packages/main/src/azure-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@nammatham/azure-functions';
1 change: 1 addition & 0 deletions packages/main/src/core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@nammatham/core';
1 change: 1 addition & 0 deletions packages/main/src/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '@nammatham/express';
3 changes: 3 additions & 0 deletions packages/main/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from '@nammatham/core';
export * from '@nammatham/express';
export * from '@nammatham/azure-functions';
Loading

0 comments on commit 7d78647

Please sign in to comment.