From ba19fa262b1d97425a53213b8151631db47c1bc2 Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Sun, 21 Jan 2024 23:38:00 +0700 Subject: [PATCH 1/7] rename NAMMATHAM_ENV to NODE_ENV --- README.md | 2 +- examples/azure-functions-minimal/package.json | 2 +- examples/azure-functions-timer-trigger/package.json | 2 +- examples/azure-functions-with-inversify/package.json | 2 +- examples/azure-functions-with-test/package.json | 2 +- examples/azure-functions-with-trpc/package.json | 2 +- examples/azure-functions/package.json | 2 +- packages/azure-functions/README.md | 2 +- packages/azure-functions/src/handler-resolver.ts | 2 +- packages/core/README.md | 2 +- packages/core/src/logger.ts | 2 +- packages/express/README.md | 2 +- packages/express/src/express-server.ts | 2 +- packages/main/README.md | 2 +- packages/trpc-azure-functions/README.md | 2 +- packages/trpc-azure-functions/src/trpc-azure-functions.ts | 2 +- 16 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 96d5f35c..1724f7b8 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/examples/azure-functions-minimal/package.json b/examples/azure-functions-minimal/package.json index 09560fd4..c61ef0ce 100644 --- a/examples/azure-functions-minimal/package.json +++ b/examples/azure-functions-minimal/package.json @@ -7,7 +7,7 @@ "build": "tsc", "lint": "tsc --noEmit", "start": "tsc && func start", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts" }, "author": "Thada Wangthammang", "license": "MIT", diff --git a/examples/azure-functions-timer-trigger/package.json b/examples/azure-functions-timer-trigger/package.json index 08aa8b9d..873fa030 100644 --- a/examples/azure-functions-timer-trigger/package.json +++ b/examples/azure-functions-timer-trigger/package.json @@ -7,7 +7,7 @@ "build": "tsc", "lint": "tsc --noEmit", "start": "tsc && func start", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts" }, "author": "Thada Wangthammang", "license": "MIT", diff --git a/examples/azure-functions-with-inversify/package.json b/examples/azure-functions-with-inversify/package.json index 40513e8d..b0028722 100644 --- a/examples/azure-functions-with-inversify/package.json +++ b/examples/azure-functions-with-inversify/package.json @@ -7,7 +7,7 @@ "build": "tsc", "lint": "tsc --noEmit", "start": "tsc && func start", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts" }, "author": "Thada Wangthammang", "license": "MIT", diff --git a/examples/azure-functions-with-test/package.json b/examples/azure-functions-with-test/package.json index 6d840fb7..5a3f155e 100644 --- a/examples/azure-functions-with-test/package.json +++ b/examples/azure-functions-with-test/package.json @@ -7,7 +7,7 @@ "build": "tsc", "lint": "tsc --noEmit", "start": "tsc && func start", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts" }, "author": "Thada Wangthammang", "license": "MIT", diff --git a/examples/azure-functions-with-trpc/package.json b/examples/azure-functions-with-trpc/package.json index 7df9cf33..728ad809 100644 --- a/examples/azure-functions-with-trpc/package.json +++ b/examples/azure-functions-with-trpc/package.json @@ -8,7 +8,7 @@ "lint": "tsc --noEmit", "start": "tsc && func start", "start:client": "tsx src/client.ts", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "dev:old": "nodemon --watch src --ext ts --exec 'npm run build'" }, "author": "Thada Wangthammang", diff --git a/examples/azure-functions/package.json b/examples/azure-functions/package.json index d96c0616..3a265e1b 100644 --- a/examples/azure-functions/package.json +++ b/examples/azure-functions/package.json @@ -7,7 +7,7 @@ "build": "tsc", "lint": "tsc --noEmit", "start": "tsc && func start", - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts" + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts" }, "author": "Thada Wangthammang", "license": "MIT", diff --git a/packages/azure-functions/README.md b/packages/azure-functions/README.md index 96d5f35c..1724f7b8 100644 --- a/packages/azure-functions/README.md +++ b/packages/azure-functions/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/packages/azure-functions/src/handler-resolver.ts b/packages/azure-functions/src/handler-resolver.ts index c7e88f1a..c62bad12 100644 --- a/packages/azure-functions/src/handler-resolver.ts +++ b/packages/azure-functions/src/handler-resolver.ts @@ -149,7 +149,7 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver { logger.debug(`Running with runtime: ${app.runtime}`); logger.debug(`runtime: ${app.runtime}, isDevelopment: ${app.isDevelopment}`); - if (app.runtime === 'express' && process.env.NAMMATHAM_ENV !== 'development') { + if (app.runtime === 'express' && process.env.NODE_ENV !== 'development') { throw new Error( `expressPlugin will not start express server in production mode for Azure Functions Adapter, because Azure Functions will start the server for us. Please make set isDevelopment to be 'false' when use expressPlugin in production mode.` diff --git a/packages/core/README.md b/packages/core/README.md index 96d5f35c..1724f7b8 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/packages/core/src/logger.ts b/packages/core/src/logger.ts index f1ece34a..8d04b539 100644 --- a/packages/core/src/logger.ts +++ b/packages/core/src/logger.ts @@ -1,5 +1,5 @@ import pino from 'pino'; -const isDevelopment = process.env.NAMMATHAM_ENV === 'development'; +const isDevelopment = process.env.NODE_ENV === 'development'; export const _logger = pino({ level: process.env.NAMMATHAM_LOG_LEVEL || 'info', diff --git a/packages/express/README.md b/packages/express/README.md index 96d5f35c..1724f7b8 100644 --- a/packages/express/README.md +++ b/packages/express/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/packages/express/src/express-server.ts b/packages/express/src/express-server.ts index 2b1efb71..ba9c7d29 100644 --- a/packages/express/src/express-server.ts +++ b/packages/express/src/express-server.ts @@ -21,7 +21,7 @@ export interface ExpressServerOption { */ export function expressPlugin(option?: ExpressServerOption) { return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => { - const isDevelopment = option?.isDevelopment ?? process.env.NAMMATHAM_ENV === 'development'; + const isDevelopment = option?.isDevelopment ?? process.env.NODE_ENV === 'development'; if (!isDevelopment) { logger.debug('Skipping express server'); return; diff --git a/packages/main/README.md b/packages/main/README.md index 96d5f35c..1724f7b8 100644 --- a/packages/main/README.md +++ b/packages/main/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/packages/trpc-azure-functions/README.md b/packages/trpc-azure-functions/README.md index 96d5f35c..1724f7b8 100644 --- a/packages/trpc-azure-functions/README.md +++ b/packages/trpc-azure-functions/README.md @@ -75,7 +75,7 @@ Then edit `package.json` like this; { "main": "dist/src/main.js", "scripts": { - "dev": "cross-env NAMMATHAM_ENV=development tsx watch src/main.ts", + "dev": "cross-env NODE_ENV=development tsx watch src/main.ts", "start": "tsc && func start" } } diff --git a/packages/trpc-azure-functions/src/trpc-azure-functions.ts b/packages/trpc-azure-functions/src/trpc-azure-functions.ts index c955d61e..b420700a 100644 --- a/packages/trpc-azure-functions/src/trpc-azure-functions.ts +++ b/packages/trpc-azure-functions/src/trpc-azure-functions.ts @@ -45,7 +45,7 @@ export function unstable__tRpcAzureFunctionsPlugin( }); app.addFunction(trpcFunction); - const isDevelopment = option?.expressPluginOption?.isDevelopment ?? process.env.NAMMATHAM_ENV === 'development'; + const isDevelopment = option?.expressPluginOption?.isDevelopment ?? process.env.NODE_ENV === 'development'; if (!isDevelopment) { logger.debug('Skipping express server in development mode (trpc)'); return; From 7504a2c23beba1ad43dcd96a18a4f3de6885dc6b Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Sun, 21 Jan 2024 23:39:42 +0700 Subject: [PATCH 2/7] fix example nammatham version --- examples/azure-functions-minimal/package.json | 2 +- .../package.json | 2 +- .../package.json | 2 +- .../azure-functions-with-test/package.json | 2 +- .../azure-functions-with-trpc/package.json | 2 +- examples/azure-functions/package.json | 2 +- pnpm-lock.yaml | 30 +++++++++---------- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/examples/azure-functions-minimal/package.json b/examples/azure-functions-minimal/package.json index c61ef0ce..5160a5aa 100644 --- a/examples/azure-functions-minimal/package.json +++ b/examples/azure-functions-minimal/package.json @@ -12,7 +12,7 @@ "author": "Thada Wangthammang", "license": "MIT", "dependencies": { - "nammatham": "2.0.0-alpha.8", + "nammatham": "2.0.0-alpha.9", "@azure/functions": "^4.1.0" }, "devDependencies": { diff --git a/examples/azure-functions-timer-trigger/package.json b/examples/azure-functions-timer-trigger/package.json index 873fa030..10e88d07 100644 --- a/examples/azure-functions-timer-trigger/package.json +++ b/examples/azure-functions-timer-trigger/package.json @@ -12,7 +12,7 @@ "author": "Thada Wangthammang", "license": "MIT", "dependencies": { - "nammatham": "2.0.0-alpha.8", + "nammatham": "2.0.0-alpha.9", "@azure/functions": "^4.1.0" }, "devDependencies": { diff --git a/examples/azure-functions-with-inversify/package.json b/examples/azure-functions-with-inversify/package.json index b0028722..1fb2ec6d 100644 --- a/examples/azure-functions-with-inversify/package.json +++ b/examples/azure-functions-with-inversify/package.json @@ -14,7 +14,7 @@ "dependencies": { "@azure/functions": "^4.1.0", "@di-extra/inversify": "^0.2.0", - "nammatham": "2.0.0-alpha.8", + "nammatham": "2.0.0-alpha.9", "inversify": "^6.0.2", "reflect-metadata": "^0.2.1" }, diff --git a/examples/azure-functions-with-test/package.json b/examples/azure-functions-with-test/package.json index 5a3f155e..c5d5f699 100644 --- a/examples/azure-functions-with-test/package.json +++ b/examples/azure-functions-with-test/package.json @@ -13,7 +13,7 @@ "license": "MIT", "dependencies": { "@azure/functions": "^4.1.0", - "nammatham": "2.0.0-alpha.8" + "nammatham": "2.0.0-alpha.9" }, "devDependencies": { "cross-env": "^7.0.3", diff --git a/examples/azure-functions-with-trpc/package.json b/examples/azure-functions-with-trpc/package.json index 728ad809..5a113c7e 100644 --- a/examples/azure-functions-with-trpc/package.json +++ b/examples/azure-functions-with-trpc/package.json @@ -15,7 +15,7 @@ "license": "MIT", "dependencies": { "@azure/functions": "^4.1.0", - "nammatham": "2.0.0-alpha.8", + "nammatham": "2.0.0-alpha.9", "@nammatham/trpc-azure-functions": "2.0.0-alpha.9", "@trpc/client": "^10.45.0", "@trpc/server": "^10.45.0", diff --git a/examples/azure-functions/package.json b/examples/azure-functions/package.json index 3a265e1b..b4d9b498 100644 --- a/examples/azure-functions/package.json +++ b/examples/azure-functions/package.json @@ -12,7 +12,7 @@ "author": "Thada Wangthammang", "license": "MIT", "dependencies": { - "nammatham": "2.0.0-alpha.8", + "nammatham": "2.0.0-alpha.9", "@azure/functions": "^4.1.0" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6a70b3a0..dd288667 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -72,7 +72,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main devDependencies: cross-env: @@ -94,7 +94,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main devDependencies: cross-env: @@ -116,7 +116,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main devDependencies: cross-env: @@ -144,7 +144,7 @@ importers: specifier: ^6.0.2 version: 6.0.2 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main reflect-metadata: specifier: ^0.2.1 @@ -169,7 +169,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main devDependencies: cross-env: @@ -191,7 +191,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 '@nammatham/trpc-azure-functions': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/trpc-azure-functions '@trpc/client': specifier: ^10.45.0 @@ -200,7 +200,7 @@ importers: specifier: ^10.45.0 version: 10.45.0 nammatham: - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../../packages/main trpc-azure-functions-adapter: specifier: 0.0.5 @@ -225,7 +225,7 @@ importers: specifier: ^4.1.0 version: 4.1.0 '@nammatham/core': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../core colorette: specifier: ^2.0.20 @@ -275,7 +275,7 @@ importers: packages/express: dependencies: '@nammatham/core': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../core '@types/express': specifier: ^4.17.21 @@ -290,13 +290,13 @@ importers: packages/main: dependencies: '@nammatham/azure-functions': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../azure-functions '@nammatham/core': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../core '@nammatham/express': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../express packages/trpc-azure-functions: @@ -305,13 +305,13 @@ importers: specifier: ^4.1.0 version: 4.1.0 '@nammatham/azure-functions': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../azure-functions '@nammatham/core': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../core '@nammatham/express': - specifier: 2.0.0-alpha.8 + specifier: 2.0.0-alpha.9 version: link:../express '@trpc/server': specifier: ^10.45.0 From 168a7ecb6975b9d8ac168bf841e1339ec2b93ce9 Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Sun, 21 Jan 2024 23:41:12 +0700 Subject: [PATCH 3/7] fix release script for bump version `nammatham` package --- scripts/libs/version.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/libs/version.ts b/scripts/libs/version.ts index 80f3d8b8..4a6df9da 100644 --- a/scripts/libs/version.ts +++ b/scripts/libs/version.ts @@ -38,7 +38,7 @@ async function modifyDependency(packagePath: string, newVersion: string) { // Replaces all @nammatham/* dependencies for (const [name, version] of Object.entries(dependencies ?? {})) { - if (name.startsWith('@nammatham/')) { + if (name.startsWith('@nammatham/' || name === 'nammatham')) { dependencies[name] = newVersion; } } From e0675d5b1db5e3c9cfe03a9cfce3d0919989235a Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Mon, 22 Jan 2024 00:16:20 +0700 Subject: [PATCH 4/7] Explicitly use of dev mode --- README.md | 4 ++- examples/azure-functions-minimal/src/main.ts | 28 ++++++++++--------- .../azure-functions-timer-trigger/src/main.ts | 3 ++ .../src/main.ts | 3 +- .../azure-functions-with-test/src/main.ts | 3 +- examples/azure-functions/src/main.ts | 3 +- .../azure-functions/src/handler-resolver.ts | 2 +- packages/core/src/nammatham-app.ts | 4 +-- packages/express/src/express-server.ts | 13 +++++---- .../src/trpc-azure-functions.ts | 4 +-- 10 files changed, 40 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 1724f7b8..fbd5b82a 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,9 @@ const helloFunction = func }); app.addFunctions(helloFunction); -app.register(expressPlugin()); + +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); ``` diff --git a/examples/azure-functions-minimal/src/main.ts b/examples/azure-functions-minimal/src/main.ts index 229645e1..f9e845e7 100644 --- a/examples/azure-functions-minimal/src/main.ts +++ b/examples/azure-functions-minimal/src/main.ts @@ -1,20 +1,22 @@ -import { AzureFunctionsAdapter, initNammatham, expressPlugin } from "nammatham"; +import { AzureFunctionsAdapter, initNammatham, expressPlugin } from 'nammatham'; 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( + 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()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions-timer-trigger/src/main.ts b/examples/azure-functions-timer-trigger/src/main.ts index e989672b..463657e6 100644 --- a/examples/azure-functions-timer-trigger/src/main.ts +++ b/examples/azure-functions-timer-trigger/src/main.ts @@ -4,8 +4,11 @@ import { app } from './nammatham'; app.addFunctions(simpleTimer); +const dev = process.env.NODE_ENV === 'development'; + app.register( expressPlugin({ + dev, allowAllFunctionsAccessByHttp: true, }) ); diff --git a/examples/azure-functions-with-inversify/src/main.ts b/examples/azure-functions-with-inversify/src/main.ts index b455628e..a116f49b 100644 --- a/examples/azure-functions-with-inversify/src/main.ts +++ b/examples/azure-functions-with-inversify/src/main.ts @@ -5,5 +5,6 @@ import { app } from './nammatham'; app.addFunctions(hello); -app.register(expressPlugin()); +const dev =process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions-with-test/src/main.ts b/examples/azure-functions-with-test/src/main.ts index 9857a2e1..46c7f2ca 100644 --- a/examples/azure-functions-with-test/src/main.ts +++ b/examples/azure-functions-with-test/src/main.ts @@ -4,5 +4,6 @@ import { app } from './nammatham'; app.addFunctions(hello); -app.register(expressPlugin()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/examples/azure-functions/src/main.ts b/examples/azure-functions/src/main.ts index 02c03d4c..f01344ea 100644 --- a/examples/azure-functions/src/main.ts +++ b/examples/azure-functions/src/main.ts @@ -5,5 +5,6 @@ import { app } from './nammatham'; app.addFunctions(blob, hello); -app.register(expressPlugin()); +const dev = process.env.NODE_ENV === 'development'; +app.register(expressPlugin({ dev })); app.start(); diff --git a/packages/azure-functions/src/handler-resolver.ts b/packages/azure-functions/src/handler-resolver.ts index c62bad12..1ffe3273 100644 --- a/packages/azure-functions/src/handler-resolver.ts +++ b/packages/azure-functions/src/handler-resolver.ts @@ -149,7 +149,7 @@ export class AzureFunctionsHandlerResolver extends BaseHandlerResolver { logger.debug(`Running with runtime: ${app.runtime}`); logger.debug(`runtime: ${app.runtime}, isDevelopment: ${app.isDevelopment}`); - if (app.runtime === 'express' && process.env.NODE_ENV !== 'development') { + if (app.runtime === 'express' && app.isDevelopment === false) { throw new Error( `expressPlugin will not start express server in production mode for Azure Functions Adapter, because Azure Functions will start the server for us. Please make set isDevelopment to be 'false' when use expressPlugin in production mode.` diff --git a/packages/core/src/nammatham-app.ts b/packages/core/src/nammatham-app.ts index a7fbfee8..dbaeb672 100644 --- a/packages/core/src/nammatham-app.ts +++ b/packages/core/src/nammatham-app.ts @@ -24,7 +24,7 @@ export class NammathamApp { * For example, expressPlugin will not start express server in production mode for Azure Functions Adapter, * because Azure Functions will start the server for us. */ - private _isDevelopment: boolean | undefined; + private _isDevelopment = false; public readonly startTime = performance.now(); constructor(public readonly handlerResolver: BaseHandlerResolver) {} @@ -37,7 +37,7 @@ export class NammathamApp { logger.debug('Registering functions...'); await this.handlerResolver.resolveRegisterHandler(this); logger.debug('All functions registered'); - console.log(`${logo()} \n`); + console.log(`${await logo()} \n`); } addEndpoint(func: NammamthamEndpoint) { diff --git a/packages/express/src/express-server.ts b/packages/express/src/express-server.ts index ba9c7d29..b8dcccc4 100644 --- a/packages/express/src/express-server.ts +++ b/packages/express/src/express-server.ts @@ -12,7 +12,7 @@ export interface ExpressServerOption { prefix?: string; port?: number; expressApp?: express.Express; - isDevelopment?: boolean; + dev?: boolean; allowAllFunctionsAccessByHttp?: boolean; } @@ -21,13 +21,16 @@ export interface ExpressServerOption { */ export function expressPlugin(option?: ExpressServerOption) { return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => { - const isDevelopment = option?.isDevelopment ?? process.env.NODE_ENV === 'development'; - if (!isDevelopment) { - logger.debug('Skipping express server'); + const isDevelopment = option?.dev ?? false; + app.setDevelopment(isDevelopment); + console.log(app.runtime, 'runtime') + console.log(isDevelopment, 'isDevelopment') + if (isDevelopment === false && app.runtime === 'azure-functions') { return; + } else { + logger.info('Starting express server in development mode'); } app.setRuntime('express'); - app.setDevelopment(isDevelopment); logger.debug(`Using plugin: expressPlugin`); startExpress( { diff --git a/packages/trpc-azure-functions/src/trpc-azure-functions.ts b/packages/trpc-azure-functions/src/trpc-azure-functions.ts index b420700a..71184768 100644 --- a/packages/trpc-azure-functions/src/trpc-azure-functions.ts +++ b/packages/trpc-azure-functions/src/trpc-azure-functions.ts @@ -45,9 +45,9 @@ export function unstable__tRpcAzureFunctionsPlugin( }); app.addFunction(trpcFunction); - const isDevelopment = option?.expressPluginOption?.isDevelopment ?? process.env.NODE_ENV === 'development'; + const isDevelopment = option?.expressPluginOption?.dev ?? false; if (!isDevelopment) { - logger.debug('Skipping express server in development mode (trpc)'); + logger.debug('Skipping express server in production mode (trpc)'); return; } From 70630dad4583940870bf4c6d75d5e3a3db042bac Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Mon, 22 Jan 2024 08:38:31 +0700 Subject: [PATCH 5/7] fix: non-http azure functions in dev mode --- packages/azure-functions/src/trigger.ts | 9 +++++++-- packages/azure-functions/src/utils.ts | 11 ++++------- packages/core/src/types.ts | 4 +++- packages/express/src/express-server.ts | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/packages/azure-functions/src/trigger.ts b/packages/azure-functions/src/trigger.ts index 18748c36..572fd339 100644 --- a/packages/azure-functions/src/trigger.ts +++ b/packages/azure-functions/src/trigger.ts @@ -82,7 +82,12 @@ export class AzureFunctionsTrigger extends BaseFunctionTrigger { timer(funcName: string, option: Omit) { return new AzureFunctionsHandler( funcName, - this.parseFunctionOption(funcName, option), + this.parseFunctionOption(funcName, { + ...option, + endpointOption: { + type: 'timer', + }, + }), funcOption => { app.timer(funcName, { ...option, @@ -100,7 +105,7 @@ export class AzureFunctionsTrigger extends BaseFunctionTrigger { endpointOption: { ...opt?.endpointOption, route: (opt?.endpointOption as HttpEndpointOption)?.route ?? funcName, - type: (opt?.endpointOption as HttpEndpointOption)?.type ?? 'http', + type: opt?.endpointOption?.type ?? 'generic', }, extraInputs: opt?.extraInputs ?? [], extraOutputs: opt?.extraOutputs ?? [], diff --git a/packages/azure-functions/src/utils.ts b/packages/azure-functions/src/utils.ts index e39d5f18..9c6f69ad 100644 --- a/packages/azure-functions/src/utils.ts +++ b/packages/azure-functions/src/utils.ts @@ -1,7 +1,7 @@ import type { AfterServerStartedMetadata, NammathamApp } from '@nammatham/core'; import { gray, yellow } from 'colorette'; -import { trimSlash } from '@nammatham/core'; +import { logger, trimSlash } from '@nammatham/core'; import type { AzureFunctionsEndpoint } from './types'; @@ -34,7 +34,6 @@ export async function printRegisteredFunctions( const methods = `[${getMethods(func).join(',')}]`; console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`); } - console.log(''); return azureFunctions; } @@ -49,12 +48,10 @@ export async function printRegisteredNonHttpFunctions( .filter(func => func.type === 'azure-functions') .filter(func => func.endpointOption?.type !== 'http') as AzureFunctionsEndpoint[]; if (azureFunctions.length === 0) return []; - console.log(`${yellow(`----------------------------------------------`)}\n`); - console.log(`\n${yellow('Non-HTTP Functions (In Develpment Mode Only):')}\n`); + console.log(`\n${yellow('Non-HTTP Functions, accessed by HTTP (dev mode):')}\n`); for (const func of azureFunctions) { - const methods = `[${getMethods(func).join(',')}]`; - console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`); + const type = `[${func.endpointOption?.type ?? 'generic'}]`; + console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.port))}`); } - console.log(''); return azureFunctions; } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 20cc832a..3dae0b6c 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -20,7 +20,9 @@ export interface GenericEndpointOption extends EndpointOptionBase, Record; + +export type EndpointOption = HttpEndpointOption | GenericEndpointOption | UnknownEndpointOption; export type WithEndpointOption = { endpointOption?: EndpointOption }; diff --git a/packages/express/src/express-server.ts b/packages/express/src/express-server.ts index b8dcccc4..c26e9476 100644 --- a/packages/express/src/express-server.ts +++ b/packages/express/src/express-server.ts @@ -71,12 +71,12 @@ export function startExpress( const endTime = performance.now(); const durationMs = Math.floor(endTime - app.startTime); logger.debug(`Server started at http://localhost:${port}`); - console.log(`${await logo()} ${gray(`ready in ${durationMs}ms`)}\n`); + console.log(`${await logo()} ${gray(`ready in ${durationMs} ms`)}\n`); console.log(`\n${blue('Express server started')}\n`); console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`); console.log(` ┃ Host ${gray('Not Available')} \n`); await handlerResolver.afterServerStarted(app, { port, allowAllFunctionsAccessByHttp }); - // console.log(`\nServer Ready \n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n`); + console.log('\n'); }); } From 4ac02363c6c0bc91c5aaf44f63ea031e0037beee Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Mon, 22 Jan 2024 08:59:48 +0700 Subject: [PATCH 6/7] feat: add hostname --- packages/azure-functions/src/utils.test.ts | 6 +++--- packages/azure-functions/src/utils.ts | 10 +++++----- packages/core/src/types.ts | 4 ++-- packages/express/src/express-server.ts | 19 ++++++++++++------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/azure-functions/src/utils.test.ts b/packages/azure-functions/src/utils.test.ts index 5ad8188e..f9e7ac4b 100644 --- a/packages/azure-functions/src/utils.test.ts +++ b/packages/azure-functions/src/utils.test.ts @@ -43,7 +43,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => { } as any; // Act - const result = utils.getFullUrl(func, 3000); + const result = utils.getFullUrl(func, 'localhost', 3000); // Assert expect(result).toEqual('http://localhost:3000/api/test'); @@ -58,7 +58,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => { } as any; // Act - const result = utils.getFullUrl(func, 3000); + const result = utils.getFullUrl(func, 'localhost', 3000); // Assert expect(result).toEqual('http://localhost:3000/api/test'); @@ -71,7 +71,7 @@ describe(`Test utils: ${utils.getFullUrl.name}`, () => { } as any; // Act - const result = utils.getFullUrl(func, 3000); + const result = utils.getFullUrl(func, '', 3000); // Assert expect(result).toEqual(''); diff --git a/packages/azure-functions/src/utils.ts b/packages/azure-functions/src/utils.ts index 9c6f69ad..e963b867 100644 --- a/packages/azure-functions/src/utils.ts +++ b/packages/azure-functions/src/utils.ts @@ -1,7 +1,7 @@ import type { AfterServerStartedMetadata, NammathamApp } from '@nammatham/core'; import { gray, yellow } from 'colorette'; -import { logger, trimSlash } from '@nammatham/core'; +import { trimSlash } from '@nammatham/core'; import type { AzureFunctionsEndpoint } from './types'; @@ -13,10 +13,10 @@ export function getMethods(func: AzureFunctionsEndpoint): stri return methods; } -export function getFullUrl(func: AzureFunctionsEndpoint, port?: number): string { +export function getFullUrl(func: AzureFunctionsEndpoint, hostname: string, port?: number): string { const endpoint = func.endpointOption?.route ?? func.name; if (typeof endpoint !== 'string') return ''; - return `http://localhost${port ? `:${port}` : ''}/api/${trimSlash(endpoint)}`; + return `http://${hostname}${port ? `:${port}` : ''}/api/${trimSlash(endpoint)}`; } export const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); @@ -32,7 +32,7 @@ export async function printRegisteredFunctions( console.log(`\n${yellow('Functions:')}\n`); for (const func of azureFunctions) { const methods = `[${getMethods(func).join(',')}]`; - console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.port))}`); + console.log(` - ${func.name} ${gray(methods)} ${gray(getFullUrl(func, option.hostname, option.port))}`); } return azureFunctions; } @@ -51,7 +51,7 @@ export async function printRegisteredNonHttpFunctions( console.log(`\n${yellow('Non-HTTP Functions, accessed by HTTP (dev mode):')}\n`); for (const func of azureFunctions) { const type = `[${func.endpointOption?.type ?? 'generic'}]`; - console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.port))}`); + console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.hostname, option.port))}`); } return azureFunctions; } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 3dae0b6c..0d5f23cd 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -20,9 +20,9 @@ export interface GenericEndpointOption extends EndpointOptionBase, Record; +// export type UnknownEndpointOption = EndpointOptionBase & Record; -export type EndpointOption = HttpEndpointOption | GenericEndpointOption | UnknownEndpointOption; +export type EndpointOption = HttpEndpointOption | GenericEndpointOption; export type WithEndpointOption = { endpointOption?: EndpointOption }; diff --git a/packages/express/src/express-server.ts b/packages/express/src/express-server.ts index c26e9476..2c7c82c3 100644 --- a/packages/express/src/express-server.ts +++ b/packages/express/src/express-server.ts @@ -11,6 +11,10 @@ import { createExpressMiddleware } from './middleware'; export interface ExpressServerOption { prefix?: string; port?: number; + /** + * @default localhost + */ + hostname?: string; expressApp?: express.Express; dev?: boolean; allowAllFunctionsAccessByHttp?: boolean; @@ -23,8 +27,6 @@ export function expressPlugin(option?: ExpressServerOption) { return (app: NammathamApp, handlerResolver: BaseHandlerResolver) => { const isDevelopment = option?.dev ?? false; app.setDevelopment(isDevelopment); - console.log(app.runtime, 'runtime') - console.log(isDevelopment, 'isDevelopment') if (isDevelopment === false && app.runtime === 'azure-functions') { return; } else { @@ -49,6 +51,7 @@ export function startExpress( logger.debug('Starting express server'); const expressApp = expressOption?.expressApp ?? express(); const port = expressOption?.port ?? 3000; + const hostname = expressOption?.hostname ?? 'localhost'; const prefix = expressOption?.prefix ?? '/api'; const allowAllFunctionsAccessByHttp = expressOption?.allowAllFunctionsAccessByHttp ?? false; @@ -66,17 +69,19 @@ export function startExpress( }) ); - expressApp.listen(port, async () => { + expressApp.listen(port, hostname, async () => { console.clear(); const endTime = performance.now(); const durationMs = Math.floor(endTime - app.startTime); - logger.debug(`Server started at http://localhost:${port}`); + const hostType = hostname === 'localhost' ? 'Local' : 'Host'; + // const host = hostname === 'localhost' ? gray('Not Available') : greenBright(`http://${hostname}:${port}`); + logger.debug(`Server started at http://${hostname}:${port}`); console.log(`${await logo()} ${gray(`ready in ${durationMs} ms`)}\n`); console.log(`\n${blue('Express server started')}\n`); - console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`); - console.log(` ┃ Host ${gray('Not Available')} \n`); + // console.log(` ┃ Local ${greenBright(`http://localhost:${port}`)}`); + console.log(` ┃ ${hostType} ${greenBright(`http://${hostname}:${port}`)} \n`); - await handlerResolver.afterServerStarted(app, { port, allowAllFunctionsAccessByHttp }); + await handlerResolver.afterServerStarted(app, { port, hostname, allowAllFunctionsAccessByHttp }); console.log('\n'); }); } From 488204d1dcd7ebccbdf2601e9057857c6db1fe54 Mon Sep 17 00:00:00 2001 From: Thada Wangthammang Date: Mon, 22 Jan 2024 09:08:29 +0700 Subject: [PATCH 7/7] fix type --- packages/azure-functions/src/trigger.ts | 7 +------ packages/azure-functions/src/utils.ts | 4 ++-- packages/core/src/types.ts | 1 + 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/packages/azure-functions/src/trigger.ts b/packages/azure-functions/src/trigger.ts index 572fd339..684ff9a4 100644 --- a/packages/azure-functions/src/trigger.ts +++ b/packages/azure-functions/src/trigger.ts @@ -82,12 +82,7 @@ export class AzureFunctionsTrigger extends BaseFunctionTrigger { timer(funcName: string, option: Omit) { return new AzureFunctionsHandler( funcName, - this.parseFunctionOption(funcName, { - ...option, - endpointOption: { - type: 'timer', - }, - }), + this.parseFunctionOption(funcName, option), funcOption => { app.timer(funcName, { ...option, diff --git a/packages/azure-functions/src/utils.ts b/packages/azure-functions/src/utils.ts index e963b867..36b2d378 100644 --- a/packages/azure-functions/src/utils.ts +++ b/packages/azure-functions/src/utils.ts @@ -50,8 +50,8 @@ export async function printRegisteredNonHttpFunctions( if (azureFunctions.length === 0) return []; console.log(`\n${yellow('Non-HTTP Functions, accessed by HTTP (dev mode):')}\n`); for (const func of azureFunctions) { - const type = `[${func.endpointOption?.type ?? 'generic'}]`; - console.log(` - ${func.name} ${gray(type)} ${gray(getFullUrl(func, option.hostname, option.port))}`); + // const type = `[${func.endpointOption?.type ?? 'generic'}]`; + console.log(` - ${func.name} ${gray(getFullUrl(func, option.hostname, option.port))}`); } return azureFunctions; } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 0d5f23cd..59c72259 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -35,6 +35,7 @@ export interface NammamthamEndpoint { export interface AfterServerStartedMetadata { port?: number; + hostname: string; allowAllFunctionsAccessByHttp?: boolean; }