|
1 | 1 | import { code, imp, joinCode, type Code } from "ts-poet"; |
2 | | -import type { MetaObject } from "@metaobjectsdev/metadata"; |
| 2 | +import { |
| 3 | + type MetaObject, |
| 4 | + OBJECT_ATTR_DISCRIMINATOR, |
| 5 | + OBJECT_ATTR_DISCRIMINATOR_VALUE, |
| 6 | +} from "@metaobjectsdev/metadata"; |
3 | 7 | import type { RenderContext } from "@metaobjectsdev/codegen-ts"; |
4 | | -import { GENERATED_HEADER, isProjection, pluralize, entityModuleSpecifier } from "@metaobjectsdev/codegen-ts"; |
| 8 | +import { |
| 9 | + GENERATED_HEADER, |
| 10 | + isProjection, |
| 11 | + pluralize, |
| 12 | + entityModuleSpecifier, |
| 13 | + isTphDiscriminatorBase, |
| 14 | + tphConcreteSubtypes, |
| 15 | +} from "@metaobjectsdev/codegen-ts"; |
5 | 16 |
|
6 | 17 | /** |
7 | 18 | * Render <Entity>.hooks.ts — query-key factory + 2 query hooks + (for non-projections) 3 mutation hooks. |
@@ -29,6 +40,11 @@ export function renderHooksFile(entity: MetaObject, ctx: RenderContext): string |
29 | 40 | entity.name, |
30 | 41 | ctx.extStyle, |
31 | 42 | ); |
| 43 | + // FR-017 Tier 3: a TPH discriminator base gets a polymorphic + per-subtype |
| 44 | + // hooks file (the subtype entities are filtered out of this generator). |
| 45 | + if (isTphDiscriminatorBase(entity, ctx.loadedRoot)) { |
| 46 | + return renderTphHooksFile(entity, ctx, entityModule); |
| 47 | + } |
32 | 48 | if (isProjection(entity)) { |
33 | 49 | return renderReadOnlyHooksFile(entity, entityModule); |
34 | 50 | } |
@@ -233,3 +249,177 @@ export function useDelete${entityName}( |
233 | 249 | `// Source metadata: ${entityName} (${entity.fqn()})\n`; |
234 | 250 | return header + entityImports.toString() + body.toString(); |
235 | 251 | } |
| 252 | + |
| 253 | +// --------------------------------------------------------------------------- |
| 254 | +// FR-017 Tier 3 — TPH discriminator base: polymorphic + per-subtype hooks. |
| 255 | +// --------------------------------------------------------------------------- |
| 256 | + |
| 257 | +function renderTphHooksFile(base: MetaObject, ctx: RenderContext, baseModule: string): string { |
| 258 | + const baseName = base.name; |
| 259 | + const lcBase = baseName.charAt(0).toLowerCase() + baseName.slice(1); |
| 260 | + const keysVar = `${lcBase}Keys`; |
| 261 | + const discField = base.ownAttr(OBJECT_ATTR_DISCRIMINATOR) as string; |
| 262 | + |
| 263 | + const useQuerySym = imp("useQuery@@tanstack/react-query"); |
| 264 | + const useMutationSym = imp("useMutation@@tanstack/react-query"); |
| 265 | + const useQueryClientSym = imp("useQueryClient@@tanstack/react-query"); |
| 266 | + const useQueryOptionsSym = imp("t:UseQueryOptions@@tanstack/react-query"); |
| 267 | + const useMutationOptionsSym = imp("t:UseMutationOptions@@tanstack/react-query"); |
| 268 | + const useQueryResultSym = imp("t:UseQueryResult@@tanstack/react-query"); |
| 269 | + const useMutationResultSym = imp("t:UseMutationResult@@tanstack/react-query"); |
| 270 | + const useEntityFetcherSym = imp("useEntityFetcher@@metaobjectsdev/tanstack"); |
| 271 | + const buildFilterQsSym = imp("buildFilterQs@@metaobjectsdev/runtime-web"); |
| 272 | + |
| 273 | + const subtypes = tphConcreteSubtypes(base, ctx.loadedRoot); |
| 274 | + |
| 275 | + // `${baseName}` imports BOTH the constants value (for $path/$apiPrefix) and the |
| 276 | + // discriminated-union type (declaration merge). Each subtype contributes its |
| 277 | + // interface type. |
| 278 | + const subImportLines = subtypes |
| 279 | + .map((s) => { |
| 280 | + const m = entityModuleSpecifier(ctx.selfTarget, ctx.entityModuleTarget, s.package, s.name, ctx.extStyle); |
| 281 | + return `import { type ${s.name} } from ${JSON.stringify(m)};`; |
| 282 | + }) |
| 283 | + .join("\n"); |
| 284 | + const entityImports: Code = code` |
| 285 | +import { ${baseName}, type ${baseName}Filter } from ${JSON.stringify(baseModule)}; |
| 286 | +${subImportLines} |
| 287 | +`; |
| 288 | + |
| 289 | + const queryKeys: Code = code` |
| 290 | +export const ${keysVar} = { |
| 291 | + all: () => [${JSON.stringify(lcBase)}] as const, |
| 292 | + lists: () => [...${keysVar}.all(), "list"] as const, |
| 293 | + list: (filter?: ${baseName}Filter) => [...${keysVar}.lists(), filter ?? {}] as const, |
| 294 | + details: () => [...${keysVar}.all(), "detail"] as const, |
| 295 | + detail: (id: number) => [...${keysVar}.details(), id] as const, |
| 296 | + subtypeLists: (sub: string) => [...${keysVar}.all(), sub, "list"] as const, |
| 297 | + subtypeList: (sub: string, filter?: ${baseName}Filter) => [...${keysVar}.subtypeLists(sub), filter ?? {}] as const, |
| 298 | + subtypeDetails:(sub: string) => [...${keysVar}.all(), sub, "detail"] as const, |
| 299 | + subtypeDetail: (sub: string, id: number) => [...${keysVar}.subtypeDetails(sub), id] as const, |
| 300 | +}; |
| 301 | +`; |
| 302 | + |
| 303 | + // Polymorphic reads — return the discriminated union. |
| 304 | + const polymorphic: Code = code` |
| 305 | +export function use${baseName}( |
| 306 | + id: number, |
| 307 | + opts?: Omit<${useQueryOptionsSym}<${baseName}>, "queryKey" | "queryFn">, |
| 308 | +): ${useQueryResultSym}<${baseName}> { |
| 309 | + const fetcher = ${useEntityFetcherSym}(); |
| 310 | + return ${useQuerySym}<${baseName}>({ |
| 311 | + queryKey: ${keysVar}.detail(id), |
| 312 | + queryFn: () => fetcher<${baseName}>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/\${id}\`), |
| 313 | + ...opts, |
| 314 | + }); |
| 315 | +} |
| 316 | +
|
| 317 | +export function use${pluralize(baseName)}( |
| 318 | + filter?: ${baseName}Filter, |
| 319 | + opts?: Omit<${useQueryOptionsSym}<${baseName}[]>, "queryKey" | "queryFn">, |
| 320 | +): ${useQueryResultSym}<${baseName}[]> { |
| 321 | + const fetcher = ${useEntityFetcherSym}(); |
| 322 | + const qs = filter ? "?" + ${buildFilterQsSym}(filter as Record<string, unknown>) : ""; |
| 323 | + return ${useQuerySym}<${baseName}[]>({ |
| 324 | + queryKey: ${keysVar}.list(filter), |
| 325 | + queryFn: () => fetcher<${baseName}[]>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}\${qs}\`), |
| 326 | + ...opts, |
| 327 | + }); |
| 328 | +} |
| 329 | +`; |
| 330 | + |
| 331 | + // Per-subtype hooks — scoped to each discriminator value's REST sub-path. |
| 332 | + const subtypeSections: Code[] = subtypes.map((sub) => { |
| 333 | + const value = sub.ownAttr(OBJECT_ATTR_DISCRIMINATOR_VALUE) as string; |
| 334 | + const seg = value.toLowerCase(); |
| 335 | + const valueLit = JSON.stringify(value); |
| 336 | + const createInput = `Omit<${sub.name}, ${JSON.stringify(discField)}>`; |
| 337 | + const updateInput = `Partial<${createInput}>`; |
| 338 | + const subPath = `\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/${seg}\``; |
| 339 | + return code` |
| 340 | +export function use${pluralize(sub.name)}( |
| 341 | + filter?: ${baseName}Filter, |
| 342 | + opts?: Omit<${useQueryOptionsSym}<${sub.name}[]>, "queryKey" | "queryFn">, |
| 343 | +): ${useQueryResultSym}<${sub.name}[]> { |
| 344 | + const fetcher = ${useEntityFetcherSym}(); |
| 345 | + const qs = filter ? "?" + ${buildFilterQsSym}(filter as Record<string, unknown>) : ""; |
| 346 | + return ${useQuerySym}<${sub.name}[]>({ |
| 347 | + queryKey: ${keysVar}.subtypeList(${valueLit}, filter), |
| 348 | + queryFn: () => fetcher<${sub.name}[]>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/${seg}\${qs}\`), |
| 349 | + ...opts, |
| 350 | + }); |
| 351 | +} |
| 352 | +
|
| 353 | +export function use${sub.name}( |
| 354 | + id: number, |
| 355 | + opts?: Omit<${useQueryOptionsSym}<${sub.name}>, "queryKey" | "queryFn">, |
| 356 | +): ${useQueryResultSym}<${sub.name}> { |
| 357 | + const fetcher = ${useEntityFetcherSym}(); |
| 358 | + return ${useQuerySym}<${sub.name}>({ |
| 359 | + queryKey: ${keysVar}.subtypeDetail(${valueLit}, id), |
| 360 | + queryFn: () => fetcher<${sub.name}>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/${seg}/\${id}\`), |
| 361 | + ...opts, |
| 362 | + }); |
| 363 | +} |
| 364 | +
|
| 365 | +export function useCreate${sub.name}( |
| 366 | + opts?: Omit<${useMutationOptionsSym}<${sub.name}, Error, ${createInput}>, "mutationFn">, |
| 367 | +): ${useMutationResultSym}<${sub.name}, Error, ${createInput}> { |
| 368 | + const fetcher = ${useEntityFetcherSym}(); |
| 369 | + const qc = ${useQueryClientSym}(); |
| 370 | + return ${useMutationSym}<${sub.name}, Error, ${createInput}>({ |
| 371 | + mutationFn: (input) => fetcher<${sub.name}>(${subPath}, { |
| 372 | + method: "POST", |
| 373 | + headers: { "Content-Type": "application/json" }, |
| 374 | + body: JSON.stringify(input), |
| 375 | + }), |
| 376 | + ...opts, |
| 377 | + onSuccess: (...args) => { |
| 378 | + qc.invalidateQueries({ queryKey: ${keysVar}.all() }); |
| 379 | + opts?.onSuccess?.(...args); |
| 380 | + }, |
| 381 | + }); |
| 382 | +} |
| 383 | +
|
| 384 | +export function useUpdate${sub.name}( |
| 385 | + opts?: Omit<${useMutationOptionsSym}<${sub.name}, Error, { id: number; input: ${updateInput} }>, "mutationFn">, |
| 386 | +): ${useMutationResultSym}<${sub.name}, Error, { id: number; input: ${updateInput} }> { |
| 387 | + const fetcher = ${useEntityFetcherSym}(); |
| 388 | + const qc = ${useQueryClientSym}(); |
| 389 | + return ${useMutationSym}({ |
| 390 | + mutationFn: ({ id, input }) => fetcher<${sub.name}>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/${seg}/\${id}\`, { |
| 391 | + method: "PATCH", |
| 392 | + headers: { "Content-Type": "application/json" }, |
| 393 | + body: JSON.stringify(input), |
| 394 | + }), |
| 395 | + ...opts, |
| 396 | + onSuccess: (...args) => { |
| 397 | + qc.invalidateQueries({ queryKey: ${keysVar}.all() }); |
| 398 | + opts?.onSuccess?.(...args); |
| 399 | + }, |
| 400 | + }); |
| 401 | +} |
| 402 | +
|
| 403 | +export function useDelete${sub.name}( |
| 404 | + opts?: Omit<${useMutationOptionsSym}<void, Error, number>, "mutationFn">, |
| 405 | +): ${useMutationResultSym}<void, Error, number> { |
| 406 | + const fetcher = ${useEntityFetcherSym}(); |
| 407 | + const qc = ${useQueryClientSym}(); |
| 408 | + return ${useMutationSym}({ |
| 409 | + mutationFn: (id) => fetcher<void>(\`\${${baseName}.$apiPrefix}\${${baseName}.$path}/${seg}/\${id}\`, { method: "DELETE" }), |
| 410 | + ...opts, |
| 411 | + onSuccess: (...args) => { |
| 412 | + qc.invalidateQueries({ queryKey: ${keysVar}.all() }); |
| 413 | + opts?.onSuccess?.(...args); |
| 414 | + }, |
| 415 | + }); |
| 416 | +} |
| 417 | +`; |
| 418 | + }); |
| 419 | + |
| 420 | + const body: Code = joinCode([queryKeys, polymorphic, ...subtypeSections], { on: "\n" }); |
| 421 | + const header = |
| 422 | + `// ${GENERATED_HEADER}-tanstack — DO NOT EDIT.\n` + |
| 423 | + `// Source metadata: ${baseName} (${base.fqn()}) — TPH discriminator base\n`; |
| 424 | + return header + entityImports.toString() + body.toString(); |
| 425 | +} |
0 commit comments