Skip to content

Commit

Permalink
refactor: refactor more lean remove azure function deps
Browse files Browse the repository at this point in the history
  • Loading branch information
mildronize committed Aug 21, 2024
1 parent 1f8c6d4 commit f063522
Show file tree
Hide file tree
Showing 13 changed files with 2,983 additions and 2,555 deletions.
9 changes: 6 additions & 3 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "2.0.1",
"private": true,
"description": "Azure Function Nodejs Lightweight framework with Dependency Injection (inversify Extension)",
"main": "dist/src/functions/*.js",
"main": "dist/src/main.js",
"scripts": {
"build": "tsc",
"lint": "tsc --noEmit",
Expand All @@ -16,8 +16,11 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.0.1",
"trpc-azure-functions-adapter": "workspace:*"
"@azure/functions": "^4.5.0",
"@trpc/server": "^10.40.0",
"@trpc/client": "^10.40.0",
"trpc-azure-functions-adapter": "workspace:*",
"zod": "^3.22.4"
},
"repository": {
"type": "git",
Expand Down
25 changes: 25 additions & 0 deletions examples/basic/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createTRPCProxyClient, httpBatchLink, loggerLink } from '@trpc/client';
import type { AppRouter } from './trpc';

const sleep = (ms = 100) => new Promise((resolve) => setTimeout(resolve, ms));

async function main() {
const url = 'http://127.0.0.1:7071/api/trpc';

const proxy = createTRPCProxyClient<AppRouter>({
links: [loggerLink(), httpBatchLink({ url })],
});

await sleep();

const greet = await proxy.greet.query();
// const greet = await proxy.greet.query({ text: 'client' });
console.log('created post', greet);
await sleep();



console.log('👌 should be a clean exit if everything is working right');
}

main().catch(console.error);
15 changes: 0 additions & 15 deletions examples/basic/src/functions/hello.ts

This file was deleted.

7 changes: 0 additions & 7 deletions examples/basic/src/functions/trpc.ts

This file was deleted.

13 changes: 13 additions & 0 deletions examples/basic/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createAzureFunctionsHandler } from 'trpc-azure-functions-adapter';
import { appRouter, createContext } from './trpc';
import { app } from '@azure/functions';

app.http('trpc', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
route: 'trpc/{x:regex(^[^\\/]+$)}',
handler: createAzureFunctionsHandler({
router: appRouter,
createContext,
}),
});
17 changes: 14 additions & 3 deletions examples/basic/src/router.ts → examples/basic/src/trpc.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { AzureFunctionsContextOption } from 'trpc-azure-functions-adapter';
import { inferAsyncReturnType, initTRPC } from '@trpc/server';
import { z } from 'zod';

export function createContext({ context, request }: AzureFunctionsContextOption) {
return {
context,
request,
data: 'hello'
};
}
type Context = inferAsyncReturnType<typeof createContext>;
Expand All @@ -14,11 +16,20 @@ const t = initTRPC.context<Context>().create();
const publicProcedure = t.procedure;

export const appRouter = t.router({
greet: publicProcedure.query(({ input, ctx }) => {
console.log(ctx.request.params);
greet: publicProcedure
// .input(z.object({
// text: z.string(),
// }))
.query(({ ctx }) => {

// console.log(input.text);
const input = { text: 'client' };

return `Greetings, `;
return `Greetings, ${input.text} `;
}),
});

export type AppRouter = typeof appRouter;



6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.8.3",
"tsup": "^6.6.3",
"@trpc/server": "^10.40.0",
"@azure/functions": "^4.5.0",
"typescript": "^5.0.2",
"zod": "^3.22.4"
},
Expand All @@ -54,9 +56,5 @@
"require": [
"esbuild-register"
]
},
"dependencies": {
"@azure/functions": "^4.0.1",
"@trpc/server": "^10.40.0"
}
}
3 changes: 1 addition & 2 deletions packages/trpc-azure-functions-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
"author": "Thada Wangthammang",
"license": "MIT",
"dependencies": {
"@azure/functions": "^4.1.0",
"@trpc/server": "^10.40.0"
"@azure/functions": "^4.1.0"
},
"repository": {
"type": "git",
Expand Down
52 changes: 27 additions & 25 deletions packages/trpc-azure-functions-adapter/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,46 +32,48 @@ export type AzureFunctionsCreateContextFn<TRouter extends AnyRouter> = ({

export type AzureFunctionsOptions<TRouter extends AnyRouter, TRequest> =
| {
router: TRouter;
batching?: {
enabled: boolean;
};
onError?: OnErrorFunction<TRouter, TRequest>;
responseMeta?: ResponseMetaFn<TRouter>;
} & (
| {
/**
* @link https://trpc.io/docs/context
**/
createContext: AzureFunctionsCreateContextFn<TRouter>;
}
| {
/**
* @link https://trpc.io/docs/context
**/
createContext?: AzureFunctionsCreateContextFn<TRouter>;
}
);
router: TRouter;
batching?: {
enabled: boolean;
};
onError?: OnErrorFunction<TRouter, TRequest>;
responseMeta?: ResponseMetaFn<TRouter>;
} & (
| {
/**
* @link https://trpc.io/docs/context
**/
createContext: AzureFunctionsCreateContextFn<TRouter>;
}
| {
/**
* @link https://trpc.io/docs/context
**/
createContext?: AzureFunctionsCreateContextFn<TRouter>;
}
);

export async function azureFunctionsContextToHttpRequest(request: AzureHttpRequest): Promise<HTTPRequest> {
const body = await request.text();
const query = new URLSearchParams();
for (const [key, value] of Object.entries(request.query ?? {})) {
console.log(`Converting Query 111`);
for (const [key, value] of request.query.entries()) {
if (typeof value !== 'undefined') {
query.append(key, value);
console.log(`Converting Query: ${key} $$$ ${value}!`);
}
}

const headers: HTTPHeaders = {};
for (const [key, value] of Object.entries(request.headers ?? {})) {
for (const [key, value] of request.headers.entries()) {
if (typeof value !== 'undefined') {
headers[key] = value;
}
}

return {
method: request.method || 'get',
query,
query: request.query as any,
headers,
body,
};
Expand All @@ -98,7 +100,7 @@ export type RequestHandlerReturn = (
context: InvocationContext
) => Promise<HttpResponseInit | AzureHttpResponse>;

export function wrapAzureFunctionsRequestHandler<TRouter extends AnyRouter>(
export function createAzureFunctionsHandler<TRouter extends AnyRouter>(
opts: AzureFunctionsOptions<TRouter, AzureHttpRequest>
): RequestHandlerReturn {
return async (request, context) => {
Expand Down Expand Up @@ -131,4 +133,4 @@ export function wrapAzureFunctionsRequestHandler<TRouter extends AnyRouter>(
export type AzureFunctionsContextOption = {
context: InvocationContext;
request: AzureHttpRequest;
};
};
38 changes: 0 additions & 38 deletions packages/trpc-azure-functions-adapter/src/azure-functions.ts

This file was deleted.

Loading

0 comments on commit f063522

Please sign in to comment.