Skip to content

Commit

Permalink
Merge pull request #21 from Busy-Hour-Studio/feat/trpc-support
Browse files Browse the repository at this point in the history
feat(rc): trpc definitions supports
  • Loading branch information
krsbx authored Sep 21, 2024
2 parents 8b8a125 + 39c6e74 commit 2be1cf6
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 11 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.

## [0.2.0-2](https://github.com/busy-hour-studio/blaze-types/compare/v0.2.0-1...v0.2.0-2) (2024-09-19)


### Bug Fixes

* remove withTrpc config ([a215ced](https://github.com/busy-hour-studio/blaze-types/commit/a215ced13b7ba1854b8d59b96a0e7e90fe3433cd))

## [0.2.0-1](https://github.com/busy-hour-studio/blaze-types/compare/v0.2.0-0...v0.2.0-1) (2024-09-18)


### Features

* **rc:** trpc definitions supports ([9fe50c3](https://github.com/busy-hour-studio/blaze-types/commit/9fe50c3ef9f1bc7b6b962d83ec0e4868a20b40dd))

## [0.2.0-0](https://github.com/busy-hour-studio/blaze-types/compare/v0.1.1...v0.2.0-0) (2024-09-13)


Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"types": "dist/types/index.d.ts",
"module": "dist/esm/index.js",
"type": "module",
"version": "0.2.0-0",
"version": "0.2.0-2",
"license": "MIT",
"scripts": {
"prebuild": "rimraf dist",
Expand All @@ -25,6 +25,7 @@
"@busy-hour/blaze": "3.3.0-2",
"@commitlint/cli": "^17.8.1",
"@commitlint/config-conventional": "^17.8.1",
"@trpc/server": "^10.45.2",
"@types/glob": "^8.1.0",
"@types/node": "^20.12.7",
"@typescript-eslint/eslint-plugin": "^6.21.0",
Expand Down
19 changes: 14 additions & 5 deletions pnpm-lock.yaml

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

20 changes: 17 additions & 3 deletions src/lib/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ function buildDefinition(info: ServiceInformation): ServiceDefinition {
const event = `
EventsExtractor<typeof ${info.fileName}>`;
const imports = `import ${info.fileName} from '${info.importPath}';`;
const trpcQuery = `
TrpcQueryExtractor<typeof ${info.fileName}>`;
const trpcMutation = `
TrpcMutationExtractor<typeof ${info.fileName}>`;

return {
action,
event,
trpcQuery,
trpcMutation,
import: imports,
};
}
Expand All @@ -32,22 +38,30 @@ function generateDefinition(definitions: ServiceDefinition[]) {
const events = isShouldExtend
? `export interface EventCallRecord extends ${definitions.map((def) => def.event).join(',')} {}`
: `export interface EventCallRecord {}`;
const trpcQuery = isShouldExtend
? `export interface TrpcQueryCallRecord extends ${definitions.map((def) => def.trpcQuery).join(',')} {}`
: '';
const trpcMutation = isShouldExtend
? `export interface TrpcMutationCallRecord extends ${definitions.map((def) => def.trpcMutation).join(',')} {}`
: '';

if (isShouldExtend) {
imports.push(...definitions.map((def) => def.import));
}

return `${DISCLAIMER}
/* eslint-disable prettier/prettier */
/* eslint-disable no-unused-vars */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable */
${imports.join('\n')}
declare module '@busy-hour/blaze' {
${actions}
${events}
${trpcQuery}
${trpcMutation}
}
`;
}
Expand Down
31 changes: 30 additions & 1 deletion src/types/extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ import type {
AnyActionHook,
AnyAfterHook,
AnyAfterHookHandler,
ActionEventCallRequest,
ActionCallResult,
} from '@busy-hour/blaze';
import type { RecordString, RecordUnknown } from './helper';
import type {
AnyRootConfig,
BuildProcedure,
ProcedureType,
} from '@trpc/server';
import type { Random, RecordString, RecordUnknown } from './helper';

export type ExtractActionValidator<
S extends Service,
Expand Down Expand Up @@ -114,3 +121,25 @@ export type ExtractEventValidator<
: unknown
: unknown
: unknown;

export type ProcedureExtractor<
P extends ProcedureType,
CR extends ActionEventCallRequest<Random, Random, Random, Random, Random>,
> = BuildProcedure<
P,
{
_config: AnyRootConfig;
_ctx_out: Random;
// eslint-disable-next-line no-use-before-define
_input_in: Omit<CR, 'result'>;
// eslint-disable-next-line no-use-before-define
_input_out: Omit<CR, 'result'>;
_meta: Random;
// eslint-disable-next-line no-use-before-define
_output_in: ActionCallResult<CR['result']>;
// eslint-disable-next-line no-use-before-define
_output_out: ActionCallResult<CR['result']>;
},
// eslint-disable-next-line no-use-before-define
ActionCallResult<CR['result']>
>;
2 changes: 2 additions & 0 deletions src/types/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export interface ServiceDefinition {
action: string;
event: string;
import: string;
trpcQuery: string;
trpcMutation: string;
}
43 changes: 42 additions & 1 deletion src/types/internal.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { ActionEventCallRequest, Service } from '@busy-hour/blaze';
import type {
ActionEventCallRequest,
AnyAction,
Service,
} from '@busy-hour/blaze';
import type { RecordString, RecordUnknown } from './helper';
import type {
ExtractActionHandler,
ExtractActionValidator,
ExtractEventValidator,
ProcedureExtractor,
} from './extractor';

export type ServiceNameExtractor<T extends Service> =
Expand Down Expand Up @@ -35,3 +40,39 @@ export type EventsExtractor<T extends Service> = {
unknown
>;
};

export type TrpcMutationExtractor<T extends Service> = {
[A in keyof T['actions'] as T['actions'][A] extends AnyAction
? // @ts-expect-error not-defined
NonNullable<T['actions'][A]['trpc']> extends 'mutation'
? `${ServiceNameExtractor<T>}.${A extends string ? A : never}`
: never
: never]: ProcedureExtractor<
'mutation',
ActionEventCallRequest<
ExtractActionValidator<T, A, 'header'>,
ExtractActionValidator<T, A, 'params'>,
ExtractActionValidator<T, A, 'query'>,
ExtractActionValidator<T, A, 'body'>,
ExtractActionHandler<T, A>
>
>;
};

export type TrpcQueryExtractor<T extends Service> = {
[A in keyof T['actions'] as T['actions'][A] extends AnyAction
? // @ts-expect-error not-defined
NonNullable<T['actions'][A]['trpc']> extends 'query'
? `${ServiceNameExtractor<T>}.${A extends string ? A : never}`
: never
: never]: ProcedureExtractor<
'query',
ActionEventCallRequest<
ExtractActionValidator<T, A, 'header'>,
ExtractActionValidator<T, A, 'params'>,
ExtractActionValidator<T, A, 'query'>,
ExtractActionValidator<T, A, 'body'>,
ExtractActionHandler<T, A>
>
>;
};
2 changes: 2 additions & 0 deletions src/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ export const IMPORTS = [
`import type {
ActionsExtractor,
EventsExtractor,
TrpcMutationExtractor,
TrpcQueryExtractor
} from '@busy-hour/blaze-types/internal';`,
];

0 comments on commit 2be1cf6

Please sign in to comment.