Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly use of dev mode #127

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand All @@ -75,7 +77,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"
}
}
Expand Down
4 changes: 2 additions & 2 deletions examples/azure-functions-minimal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"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",
"dependencies": {
"nammatham": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.9",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
28 changes: 15 additions & 13 deletions examples/azure-functions-minimal/src/main.ts
Original file line number Diff line number Diff line change
@@ -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();
4 changes: 2 additions & 2 deletions examples/azure-functions-timer-trigger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"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",
"dependencies": {
"nammatham": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.9",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions examples/azure-functions-timer-trigger/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { app } from './nammatham';

app.addFunctions(simpleTimer);

const dev = process.env.NODE_ENV === 'development';

app.register(
expressPlugin({
dev,
allowAllFunctionsAccessByHttp: true,
})
);
Expand Down
4 changes: 2 additions & 2 deletions examples/azure-functions-with-inversify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
"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",
"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"
},
Expand Down
3 changes: 2 additions & 1 deletion examples/azure-functions-with-inversify/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions examples/azure-functions-with-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"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",
"dependencies": {
"@azure/functions": "^4.1.0",
"nammatham": "2.0.0-alpha.8"
"nammatham": "2.0.0-alpha.9"
},
"devDependencies": {
"cross-env": "^7.0.3",
Expand Down
3 changes: 2 additions & 1 deletion examples/azure-functions-with-test/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
4 changes: 2 additions & 2 deletions examples/azure-functions-with-trpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"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",
"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",
Expand Down
4 changes: 2 additions & 2 deletions examples/azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
"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",
"dependencies": {
"nammatham": "2.0.0-alpha.8",
"nammatham": "2.0.0-alpha.9",
"@azure/functions": "^4.1.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion examples/azure-functions/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion packages/azure-functions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-functions/src/handler-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
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' && app.isDevelopment === false) {

Check warning on line 152 in packages/azure-functions/src/handler-resolver.ts

View check run for this annotation

Codecov / codecov/patch

packages/azure-functions/src/handler-resolver.ts#L152

Added line #L152 was not covered by tests
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.`
Expand Down
2 changes: 1 addition & 1 deletion packages/azure-functions/src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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 ?? [],
Expand Down
6 changes: 3 additions & 3 deletions packages/azure-functions/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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('');
Expand Down
15 changes: 6 additions & 9 deletions packages/azure-functions/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export function getMethods(func: AzureFunctionsEndpoint<unknown, unknown>): stri
return methods;
}

export function getFullUrl(func: AzureFunctionsEndpoint<unknown, unknown>, port?: number): string {
export function getFullUrl(func: AzureFunctionsEndpoint<unknown, unknown>, 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));
Expand All @@ -32,9 +32,8 @@ 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))}`);
}
console.log('');
return azureFunctions;
}

Expand All @@ -49,12 +48,10 @@ export async function printRegisteredNonHttpFunctions(
.filter(func => func.type === 'azure-functions')
.filter(func => func.endpointOption?.type !== 'http') as AzureFunctionsEndpoint<unknown, unknown>[];
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(getFullUrl(func, option.hostname, option.port))}`);
}
console.log('');
return azureFunctions;
}
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pino from 'pino';
const isDevelopment = process.env.NAMMATHAM_ENV === 'development';
const isDevelopment = process.env.NODE_ENV === 'development';

Check warning on line 2 in packages/core/src/logger.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/logger.ts#L2

Added line #L2 was not covered by tests

export const _logger = pino({
level: process.env.NAMMATHAM_LOG_LEVEL || 'info',
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/nammatham-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* 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;

Check warning on line 27 in packages/core/src/nammatham-app.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/nammatham-app.ts#L27

Added line #L27 was not covered by tests
public readonly startTime = performance.now();

constructor(public readonly handlerResolver: BaseHandlerResolver) {}
Expand All @@ -37,7 +37,7 @@
logger.debug('Registering functions...');
await this.handlerResolver.resolveRegisterHandler(this);
logger.debug('All functions registered');
console.log(`${logo()} \n`);
console.log(`${await logo()} \n`);

Check warning on line 40 in packages/core/src/nammatham-app.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/nammatham-app.ts#L40

Added line #L40 was not covered by tests
}

addEndpoint(func: NammamthamEndpoint) {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
type: 'generic';
}

// export type UnknownEndpointOption = EndpointOptionBase & Record<string, unknown>;

Check warning on line 24 in packages/core/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/types.ts#L23-L24

Added lines #L23 - L24 were not covered by tests
export type EndpointOption = HttpEndpointOption | GenericEndpointOption;

export type WithEndpointOption = { endpointOption?: EndpointOption };
Expand All @@ -33,6 +35,7 @@

export interface AfterServerStartedMetadata {
port?: number;
hostname: string;

Check warning on line 38 in packages/core/src/types.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/types.ts#L38

Added line #L38 was not covered by tests
allowAllFunctionsAccessByHttp?: boolean;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/express/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Expand Down
Loading