Skip to content

Commit

Permalink
feat(nammatham): handle functions metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed May 15, 2024
1 parent 042ed9e commit 263ecee
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 26 deletions.
6 changes: 4 additions & 2 deletions packages/main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"esbuild": "^0.20.2",
"execa": "^8.0.1",
"invariant": "^2.2.4",
"lodash.camelcase": "^4.3.0",
"lodash.merge": "^4.6.2",
"pino": "^8.17.1",
"pino-dev": "^4.0.3",
Expand All @@ -59,7 +60,8 @@
},
"devDependencies": {
"@types/debug": "^4.1.12",
"hono": "^4.3.6",
"@types/lodash.merge": "^4.6.9"
"@types/lodash.camelcase": "^4.3.9",
"@types/lodash.merge": "^4.6.9",
"hono": "^4.3.6"
}
}
20 changes: 14 additions & 6 deletions packages/main/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,41 @@ import type { MiddlewareHandler } from 'hono/types';

import { createMiddleware } from 'hono/factory';

import type { HttpTriggerOptions, InvocationContext } from './types';
import type { HttpTriggerOptions, InvocationContext, NammathamTrigger } from './types';

import { NammathamBase } from './nammatham';
import { Nammatham } from '../dist/main';

type HonoEnv = {
Variables: {
context: InvocationContext;
};
};

export class HonoAzureMiddleware extends NammathamBase {
export class HonoAzureMiddleware implements NammathamTrigger {
public readonly nammatham: Nammatham;

constructor(nammatham?: Nammatham) {
this.nammatham = nammatham ?? new Nammatham();
}

public get<T extends string>(route: T) {
return this.http({
method: ['GET'],
methods: ['GET'],
authLevel: 'anonymous',
route,
});
}

public post<T extends string>(route: T) {
return this.http({
method: ['POST'],
methods: ['POST'],
authLevel: 'anonymous',
route,
});
}

http<const TRoute extends string>(options: HttpTriggerOptions<TRoute>): [TRoute, MiddlewareHandler<HonoEnv>] {
this.http(options);
this.nammatham.http(options);
const middleware = createMiddleware<HonoEnv>(async (c, next) => {
const logMessages: string[] = [];
c.set('context', {
Expand Down
51 changes: 36 additions & 15 deletions packages/main/src/nammatham.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,51 @@
import type { HttpTriggerOptions } from '../dist/main';
import camelCase from 'lodash.camelcase';

export class NammathamBase {
protected functions: Record<string, any>[] = [];
import type { NammathamFunction, HttpTriggerOptions, NammathamTrigger } from './types';

public getFunctions() {
return this.functions;
}
}
export class Nammatham implements NammathamTrigger {
protected functions: NammathamFunction[] = [];

export class Nammatham extends NammathamBase {
public get<T extends string>(options: HttpTriggerOptions<T>) {
public get<T extends string>(route: T) {
return this.http({
...options,
method: ['GET'],
route,
methods: ['GET'],
});
}

public post<T extends string>(options: HttpTriggerOptions<T>) {
public post<T extends string>(route: T) {
return this.http({
...options,
method: ['POST'],
route,
methods: ['POST'],
});
}

public http<T extends string>(options: HttpTriggerOptions<T>) {
this.functions.push(options);
if (options.route === undefined && options.name === undefined) {
throw new Error('Route or Name is required');
}
this.functions.push({
name: camelCase(options.name ?? options.route),
bindings: [
{
type: 'httpTrigger',
// TODO: Add Support default value by configuring in options
authLevel: options.authLevel ?? 'function',
route: options.route,
direction: 'in',
name: 'req',
methods: options.methods ?? ['GET', 'POST', 'PUT', 'DELETE'],
},
{
type: 'http',
direction: 'out',
name: 'res',
},
],
});
return this;
}

public getFunctions() {
return this.functions;
}
}
4 changes: 2 additions & 2 deletions packages/main/src/register.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { NammathamBase } from './nammatham';
import type { NammathamTrigger } from './types';

export type FetchCallback = (request: Request, env: Record<string, unknown>) => Promise<Response> | Response;

export interface NammathamRegisterOptions {
fetch: FetchCallback;
func: NammathamBase;
func: NammathamTrigger;
}

export function register(option: NammathamRegisterOptions) {
Expand Down
17 changes: 16 additions & 1 deletion packages/main/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import type { HandlerResponse } from 'hono/types';

export interface AzureFunctionBindings extends Record<string, unknown> {
type: string;
direction: 'in' | 'out';
name: string;
}

export interface NammathamFunction {
name: string;
metadata?: Record<string, unknown>;
bindings: AzureFunctionBindings[];
}

export type HttpMethods = 'GET' | 'POST' | 'PUT' | 'DELETE';

export interface HttpTriggerOptions<TRoute extends string = string> {
method: HttpMethods[];
name?: string;
methods: HttpMethods[];
authLevel?: 'anonymous' | 'function' | 'admin';
inputs?: Record<string, unknown>;
outputs?: Record<string, unknown>;
Expand All @@ -20,5 +33,7 @@ export interface InvocationContext {
export type TriggerMetadata = Record<string, unknown>;

export interface NammathamTrigger {
get<T extends string>(route: T): unknown;
post<T extends string>(route: T): unknown;
http(options: HttpTriggerOptions): unknown;
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 263ecee

Please sign in to comment.