From 42d3098aabcaf094313d59823c0f016fa1da82ca Mon Sep 17 00:00:00 2001 From: Ashot Nazaryan Date: Mon, 9 Oct 2023 04:04:10 -0700 Subject: [PATCH 1/3] fix: HookContext is now typed in schema --- .../src/service/templates/schema.json.tpl.ts | 12 +++++++----- .../src/service/templates/schema.typebox.tpl.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/packages/generators/src/service/templates/schema.json.tpl.ts b/packages/generators/src/service/templates/schema.json.tpl.ts index 531dfff7cc..5e10d65058 100644 --- a/packages/generators/src/service/templates/schema.json.tpl.ts +++ b/packages/generators/src/service/templates/schema.json.tpl.ts @@ -15,6 +15,7 @@ const authFieldsTemplate = (authStrategies: string[]) => const template = ({ camelName, upperName, + fileName, relative, authStrategies, isEntityService, @@ -35,6 +36,7 @@ import type { HookContext } from '${relative}/declarations' import { dataValidator, queryValidator } from '${relative}/${ fileExists(cwd, lib, 'schemas') ? 'schemas/' : '' // This is for legacy backwards compatibility }validators' +import {${upperName}Service} from './${fileName}.class' // Main data model schema export const ${camelName}Schema = { @@ -54,9 +56,9 @@ export const ${camelName}Schema = { } as const export type ${upperName} = FromSchema export const ${camelName}Validator = getValidator(${camelName}Schema, dataValidator) -export const ${camelName}Resolver = resolve<${upperName}, HookContext>({}) +export const ${camelName}Resolver = resolve<${upperName}, HookContext<${upperName}Service>>({}) -export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext>({ +export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext<${upperName}Service>>({ ${localTemplate( authStrategies, `// The password should never be visible externally @@ -76,7 +78,7 @@ export const ${camelName}DataSchema = { } as const export type ${upperName}Data = FromSchema export const ${camelName}DataValidator = getValidator(${camelName}DataSchema, dataValidator) -export const ${camelName}DataResolver = resolve<${upperName}Data, HookContext>({ +export const ${camelName}DataResolver = resolve<${upperName}Data, HookContext<${upperName}Service>>({ ${localTemplate(authStrategies, `password: passwordHash({ strategy: 'local' })`)} }) @@ -92,7 +94,7 @@ export const ${camelName}PatchSchema = { } as const export type ${upperName}Patch = FromSchema export const ${camelName}PatchValidator = getValidator(${camelName}PatchSchema, dataValidator) -export const ${camelName}PatchResolver = resolve<${upperName}Patch, HookContext>({ +export const ${camelName}PatchResolver = resolve<${upperName}Patch, HookContext<${upperName}Service>>({ ${localTemplate(authStrategies, `password: passwordHash({ strategy: 'local' })`)} }) @@ -107,7 +109,7 @@ export const ${camelName}QuerySchema = { } as const export type ${upperName}Query = FromSchema export const ${camelName}QueryValidator = getValidator(${camelName}QuerySchema, queryValidator) -export const ${camelName}QueryResolver = resolve<${upperName}Query, HookContext>({ +export const ${camelName}QueryResolver = resolve<${upperName}Query, HookContext<${upperName}Service>>({ ${ isEntityService ? ` diff --git a/packages/generators/src/service/templates/schema.typebox.tpl.ts b/packages/generators/src/service/templates/schema.typebox.tpl.ts index 054e931397..20a083eae7 100644 --- a/packages/generators/src/service/templates/schema.typebox.tpl.ts +++ b/packages/generators/src/service/templates/schema.typebox.tpl.ts @@ -15,6 +15,7 @@ const authFieldsTemplate = (authStrategies: string[]) => const template = ({ camelName, upperName, + fileName, relative, authStrategies, isEntityService, @@ -36,6 +37,7 @@ import type { HookContext } from '${relative}/declarations' import { dataValidator, queryValidator } from '${relative}/${ fileExists(cwd, lib, 'schemas') ? 'schemas/' : '' // This is for legacy backwards compatibility }validators' +import {${upperName}Service} from './${fileName}.class' // Main data model schema export const ${camelName}Schema = Type.Object({ @@ -44,9 +46,9 @@ export const ${camelName}Schema = Type.Object({ }, { $id: '${upperName}', additionalProperties: false }) export type ${upperName} = Static export const ${camelName}Validator = getValidator(${camelName}Schema, dataValidator) -export const ${camelName}Resolver = resolve<${upperName}, HookContext>({}) +export const ${camelName}Resolver = resolve<${upperName}, HookContext<${upperName}Service>>({}) -export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext>({ +export const ${camelName}ExternalResolver = resolve<${upperName}, HookContext<${upperName}Service>>({ ${localTemplate( authStrategies, `// The password should never be visible externally @@ -66,7 +68,7 @@ export const ${camelName}DataSchema = Type.Pick(${camelName}Schema, [ }) export type ${upperName}Data = Static export const ${camelName}DataValidator = getValidator(${camelName}DataSchema, dataValidator) -export const ${camelName}DataResolver = resolve<${upperName}, HookContext>({ +export const ${camelName}DataResolver = resolve<${upperName}, HookContext<${upperName}Service>>({ ${localTemplate(authStrategies, `password: passwordHash({ strategy: 'local' })`)} }) @@ -76,7 +78,7 @@ export const ${camelName}PatchSchema = Type.Partial(${camelName}Schema, { }) export type ${upperName}Patch = Static export const ${camelName}PatchValidator = getValidator(${camelName}PatchSchema, dataValidator) -export const ${camelName}PatchResolver = resolve<${upperName}, HookContext>({ +export const ${camelName}PatchResolver = resolve<${upperName}, HookContext<${upperName}Service>>({ ${localTemplate(authStrategies, `password: passwordHash({ strategy: 'local' })`)} }) @@ -95,7 +97,7 @@ export const ${camelName}QuerySchema = Type.Intersect([ ], { additionalProperties: false }) export type ${upperName}Query = Static export const ${camelName}QueryValidator = getValidator(${camelName}QuerySchema, queryValidator) -export const ${camelName}QueryResolver = resolve<${upperName}Query, HookContext>({ +export const ${camelName}QueryResolver = resolve<${upperName}Query, HookContext<${upperName}Service>>({ ${ isEntityService ? ` From 1c8dbd319db516f14885f8a01d12e0986a26d752 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 10 Oct 2023 20:06:50 -0700 Subject: [PATCH 2/3] Import as type only --- packages/generators/src/service/templates/schema.json.tpl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/generators/src/service/templates/schema.json.tpl.ts b/packages/generators/src/service/templates/schema.json.tpl.ts index 5e10d65058..1be7633af8 100644 --- a/packages/generators/src/service/templates/schema.json.tpl.ts +++ b/packages/generators/src/service/templates/schema.json.tpl.ts @@ -36,7 +36,7 @@ import type { HookContext } from '${relative}/declarations' import { dataValidator, queryValidator } from '${relative}/${ fileExists(cwd, lib, 'schemas') ? 'schemas/' : '' // This is for legacy backwards compatibility }validators' -import {${upperName}Service} from './${fileName}.class' +import type { ${upperName}Service } from './${fileName}.class' // Main data model schema export const ${camelName}Schema = { From 12c679b4bf177770c6674dbd157f39000b3bd088 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 10 Oct 2023 20:07:16 -0700 Subject: [PATCH 3/3] Import as type only --- packages/generators/src/service/templates/schema.typebox.tpl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/generators/src/service/templates/schema.typebox.tpl.ts b/packages/generators/src/service/templates/schema.typebox.tpl.ts index 20a083eae7..44687b740d 100644 --- a/packages/generators/src/service/templates/schema.typebox.tpl.ts +++ b/packages/generators/src/service/templates/schema.typebox.tpl.ts @@ -37,7 +37,7 @@ import type { HookContext } from '${relative}/declarations' import { dataValidator, queryValidator } from '${relative}/${ fileExists(cwd, lib, 'schemas') ? 'schemas/' : '' // This is for legacy backwards compatibility }validators' -import {${upperName}Service} from './${fileName}.class' +import type { ${upperName}Service } from './${fileName}.class' // Main data model schema export const ${camelName}Schema = Type.Object({