diff --git a/apps/nammatham/blog/2024-05-08-release-nammatham-doc-site.md b/apps/nammatham/blog/2019-05-29-long-blog-post.md similarity index 100% rename from apps/nammatham/blog/2024-05-08-release-nammatham-doc-site.md rename to apps/nammatham/blog/2019-05-29-long-blog-post.md diff --git a/apps/nammatham/docs/community/local-dev.md b/apps/nammatham/docs/community/local-dev.md index 74ea999..84ee90c 100644 --- a/apps/nammatham/docs/community/local-dev.md +++ b/apps/nammatham/docs/community/local-dev.md @@ -4,16 +4,17 @@ sidebar_position: 1 # Local Development -```sh -npx nx build nammatham -# or -npx nx run nammatham:build -npx nx run nammatham:dev +## Local Dev Setup -npx nx run nammatham:test:watch - - -# Publish npm with nx -pnpm --filter nammatham publish +```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 ``` - diff --git a/apps/nammatham/docs/intro.md b/apps/nammatham/docs/intro.md index ae0c61b..f2760e6 100644 --- a/apps/nammatham/docs/intro.md +++ b/apps/nammatham/docs/intro.md @@ -2,263 +2,128 @@ sidebar_position: 1 --- -:::warning - -This is documentation for Nammatham 1.x, which is no longer actively maintained. - -For up-to-date documentation, see the [latest version](https://github.com/thaitype/nammatham/blob/main/README.md) (2.x). - -::: - - - +# Introduction
+Type-safe Serverless Library for Azure Functions and friends
-# Introduction - -Azure Function Lightweight frameworks with DI, providing type safe function triggers and bindings - -[![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg)](https://github.com/thaitype/nammatham/actions/workflows/test.yml?branch=v1.x) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/main/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) [![npm version](https://img.shields.io/npm/v/nammatham)](https://www.npmjs.com/package/nammatham) [![npm download](https://img.shields.io/npm/dt/nammatham)](https://www.npmjs.com/package/nammatham) -| Version | Status | Azure Functions Node.js Lib | 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?branch=v1.x)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/v1.x/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) | -| v2.x | Alpha | v4.x | [main][main] | [![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg?branch=main.unittest)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/main/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) | + [![NPM Version (with dist tag)](https://img.shields.io/npm/v/nammatham/alpha)](https://www.npmjs.com/package/nammatham) [![npm download](https://img.shields.io/npm/dt/nammatham)](https://www.npmjs.com/package/nammatham) -[v1.x]: https://github.com/thaitype/nammatham/tree/v1.x -[main]: https://github.com/thaitype/nammatham/tree/main +> 🚧 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. [Tracking v2 Roadmap](https://github.com/thaitype/nammatham/issues?q=is%3Aissue+is%3Aopen+label%3Av2-blocker) > > Note: [Nammatham v1](https://www.npmjs.com/package/nammatham) is currently in maintenance mode. no new features are actively being developed +You're reading v2 docs + + +| Version | Status | Azure Functions Node.js Lib | 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?branch=v1.x)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/v1.x/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) | +| v2.x | Alpha | v4.x | [main][main] | [![Build & Test](https://github.com/thaitype/nammatham/actions/workflows/test.yml/badge.svg?branch=main.unittest)](https://github.com/thaitype/nammatham/actions/workflows/test.yml) [![codecov](https://codecov.io/gh/thaitype/nammatham/branch/main/graph/badge.svg?token=Y7ZMDKFPAN)](https://codecov.io/gh/thaitype/nammatham) | + +[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 Lightweight framework with Dependency Injection. Provide type safety wrapping `function.json` +Nammatham (นามธรรม in Thai, pronounced `/naam ma tham/`, means **abstract** in Thai) is Azure Function Nodejs. -## Talks -Empowering TypeScript on Azure Functions with Nammatham, Azure Open Source Day @ Microsoft Thailand, 25 Mar 2023 -[![](./img/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 +## Getting Started for Azure Functions -## Compatibility with Azure Functions -- [Azure Function NodeJs](https://github.com/Azure/azure-functions-nodejs-worker/) : v3.x (`@azure/functions`) -- [Runtime Version](https://docs.microsoft.com/azure/azure-functions/functions-versions): 4 ([Source Code](https://github.dev/Azure/azure-functions-host/tree/release/4.x)) -- Node.js Versions: 16, 18 +### Install +```bash +# Including all packages +npm install nammatham@alpha +``` -## Introduction +### Example -**Nammatham** is a framework that allows you to use Azure Functions with TypeScript and decorators. It provides pre-defined JSON binding objects and utility functions, such as `httpTrigger`, to make it easier to create Azure Functions. +You can see [examples](https://github.com/thaitype/nammatham/tree/main/examples) or follow the minimal app getting started below: -Example: +> `initNammatham.create()` is a factory function for creating Nammatham App, it's a wrapper for Azure Functions App. -```ts -import { AuthorizationLevel, BaseFunction, functionName, httpTrigger } from "nammatham"; -import { HttpRequest } from "@azure/functions"; +```typescript +import { initNammatham, expressPlugin } from 'nammatham'; -@functionName("GetUsers", httpTrigger(AuthorizationLevel.Anonymous, ["get"])) -export class UserFunction extends BaseFunction { +const n = initNammatham.create(); +const func = n.func; +const app = n.app; - public override execute(req: HttpRequest): void { - const name = req.query.name; - this.res.send(`hello get user with ${name}`); - } -} -``` +const helloFunction = func + .httpGet('hello', { + route: 'hello-world', + }) + .handler(async c => { + c.context.log('HTTP trigger function processed a request.'); + c.context.debug(`Http function processed request for url "${c.trigger.url}"`); + const name = c.trigger.query.get('name') || (await c.trigger.text()) || 'world'; + return c.text(`Hello, ${name}!`); + }); -## Features -- Simple Framework, support all functionality with Azure Functions features -- Provide type utiltiy wrapping around `function.json` -- Better project orgnization -- Bindings & Triggers Built-in type support - - Http - - Timer - - Cosmos DB, support both [v2](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-trigger?tabs=in-process%2Cfunctionsv2&pivots=programming-language-javascript#configuration) and [v4](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-trigger?tabs=in-process%2Cextensionv4&pivots=programming-language-javascript#configuration). - - Blob - - Service Bus - - If your binding type isn't in the list, please use [custom type](./concept/define-azure-function.md#custom-binding) - - You can see [All available type support](./concept/binding-type) or create a PR ;). -- Support TypeScript and [InversifyJS](https://github.com/inversify/Inversify) as the [Most Popular Inversion of Control Container and Dependency Injection Approach](https://npmtrends.com/awilix-vs-bottlejs-vs-inversify-vs-node-dependency-injection-vs-tsyringe-vs-typedi-vs-typescript-ioc) -- Build Tool Agnostic, this framework just provide the library. It can work with all TypeScript build tool e.g. tsc, esbuild, etc. - - -## Installation -You can install nammatham using npm: +app.addFunctions(helloFunction); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); +app.start(); ``` -npm install nammatham inversify reflect-metadata --save -``` -For the [InversifyJS](https://github.com/inversify/InversifyJS#-installation), please refer the documentation for usage. - -## Starter Project - -- [Basic Nammtham Starter Template](https://github.com/mildronize/nammatham-starter) - -## Getting Started - -Full examples please, go to [examples](https://github.com/thaitype/nammatham/tree/v1.x/examples) directory - -### 1. Basic - -This is basic to use partially type support, you can follow steps below: - -1. define `startup.ts` (or any name) - ```ts - // File: src/startup.ts - import 'reflect-metadata'; - import { NammathamApp } from 'nammatham'; - import { SampleHttpFunction } from './functions/sample-http.function'; - - const builder = NammathamApp.createBuilder(__filename); - builder.addFunctions(SampleHttpFunction); - builder.build(); - - export default builder.getApp(); - ``` - -2. Write a function handler, extend with `BaseFunction` we will auto inject Azure Function's Context - ```ts - // src/functions/sample-http.function.ts - import { AuthorizationLevel, BaseFunction, functionName, httpTrigger } from 'nammatham'; - import { HttpRequest } from '@azure/functions'; - - @functionName('SampleHttp', httpTrigger(AuthorizationLevel.Anonymous, ['get'])) - export class SampleHttpFunction extends BaseFunction { - public override execute(req: HttpRequest): void { - const name = req.query.name; - const message = `hello get user with ${name}`; - this.context.log(message); - this.res.send(message); - } - } - ``` -3. Add Azure Functions files at root - - `host.json` - - `local.settings.json` -4. Run `ts-node` to generate all Azure Functions - ```ts - export nammatham_env=build; ts-node src/startup.ts && tsc - ``` -5. Start Azure Functions - ``` - func start - ``` - -### 2. Handle `function.json` config by yourself - -This method will support full support type when bindings config is set, for example below: - -you can define your own `function.json` in Typescript object (as you can see the variable `bindings`), this will binding type into `this.context.bindings`. - -```ts -import { BaseFunction, binding, functionName } from 'nammatham'; - -const bindings = [ - binding.httpTrigger({ name: 'req' as const }), // make string to literal type - binding.http({ name: 'res' as const }), // make string to literal type -] as const; - -@functionName('GetUser', ...bindings) -export class UserFunction extends BaseFunction{siteConfig.tagline}