A platform for community discussion. Free, open, simple.
DEV: Match store model names the way the resolver does (#45168)
Sérgio Saquetimbe24f548b8d5
diff --git a/docs/developer-guides/docs/03-code-internals/27-types.md b/docs/developer-guides/docs/03-code-internals/27-types.mdindex cee7eb33b6e..6ca100ec2cd 100644--- a/docs/developer-guides/docs/03-code-internals/27-types.md+++ b/docs/developer-guides/docs/03-code-internals/27-types.md@@ -51,7 +51,7 @@ With this file in place, `topic.sticker_count` is a `number | undefined`, and `s- Type each field from what the server sends. Make it optional unless every payload that builds the model includes it, and add `| null` when the serializer can return `nil`.- The file must be a module, so keep at least one `import type` or an `export {};`. Without one, the `declare module` blocks replace the core modules instead of extending them. Avoid a bare `import "discourse/models/topic";` for this, because it stays in the bundle as a runtime import.- Use a `.ts` file, not `.d.ts`. Type checking skips declaration files, so a field that clashes with the type core declares is only reported from a `.ts` file. The file compiles to an empty module.-- Key the registries by the exact string the plugin passes to the store. Names that aren't registered fall back to `RestModel`.+- Registry keys and lookup names are matched after the resolver's model name normalization. For example, `postStream`, `post_stream` and `post-stream` match the same key. The exported `NormalizeModelName` type describes this normalization. Keys that normalize to the same name produce a union of their registered types. Names without a matching registration and dynamic strings fall back to `RestModel`. Only the model class lookup is normalized. The identity map and `__type` retain the raw string, and request URLs are still derived from that raw string through the adapter.## Usagediff --git a/frontend/discourse/app/services/store.ts b/frontend/discourse/app/services/store.tsindex da1b5fb8161..6e5de75c9dc 100644--- a/frontend/discourse/app/services/store.ts+++ b/frontend/discourse/app/services/store.ts@@ -40,16 +40,82 @@ export interface ModelCreateAttrsRegistry {topic: TopicCreateAttrs<Topic>;}+type ModelNameLowercaseLetter =+ | "a"+ | "b"+ | "c"+ | "d"+ | "e"+ | "f"+ | "g"+ | "h"+ | "i"+ | "j"+ | "k"+ | "l"+ | "m"+ | "n"+ | "o"+ | "p"+ | "q"+ | "r"+ | "s"+ | "t"+ | "u"+ | "v"+ | "w"+ | "x"+ | "y"+ | "z";++type ModelNameDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";++/** Keep original casing for the resolver's ASCII boundary rule, then lowercase once. */+type NormalizeModelNameCharacters<+ Name extends string,+ Previous extends string = "",+ Result extends string = "",+> = Name extends `${infer First}${infer Rest}`+ ? NormalizeModelNameCharacters<+ Rest,+ First,+ `${Result}${Previous extends ModelNameLowercaseLetter | ModelNameDigit+ ? First extends Uppercase<ModelNameLowercaseLetter>+ ? "-"+ : ""+ : ""}${First extends "." ? "/" : First extends "_" | " " ? "-" : First}`+ >+ : Lowercase<Result>;++/**+ * Normalizes literal model names as the resolver does. Open-ended strings remain+ * string because they have no required property keys and cannot prove a match.+ */+export type NormalizeModelName<K extends string> = K extends unknown+ ? Record<never, never> extends Record<K, never>+ ? string+ : NormalizeModelNameCharacters<+ K extends `${infer Name}:${string}` ? Name : K+ >+ : never;++type NormalizedModelRegistry<Registry> = {+ [K in keyof Registry & string as NormalizeModelName<K>]: Registry[K];+};+/** Unregistered names, including dynamic strings, fall back to the base REST model. */-export type ModelFor<K extends string> = K extends keyof ModelRegistry- ? ModelRegistry[K]- : RestModel;+export type ModelFor<K extends string> = K extends unknown+ ? NormalizeModelName<K> extends keyof NormalizedModelRegistry<ModelRegistry>+ ? NormalizedModelRegistry<ModelRegistry>[NormalizeModelName<K>]+ : RestModel+ : never;/** Uses registered creation attributes, falling back to the model's declared fields. */-export type ModelCreateAttrsFor<K extends string> =- K extends keyof ModelCreateAttrsRegistry- ? ModelCreateAttrsRegistry[K]- : RestModelAttrs<ModelFor<K>>;+export type ModelCreateAttrsFor<K extends string> = K extends unknown+ ? NormalizeModelName<K> extends keyof NormalizedModelRegistry<ModelCreateAttrsRegistry>+ ? NormalizedModelRegistry<ModelCreateAttrsRegistry>[NormalizeModelName<K>]+ : RestModelAttrs<ModelFor<K>>+ : never;/** The content property is supplied by the legacy array proxy at runtime. */export type ResultSetOf<M> = ResultSet & { content: M[] };@@ -276,7 +342,11 @@ export default class StoreService extends Service {opts?: object): Promise<ModelFor<K> | ResultSetOf<ModelFor<K>> | undefined>;- find(type: string, findArgs?: FindArgs, opts?: object) {+ find(+ type: string,+ findArgs?: FindArgs,+ opts?: object+ ): Promise<RestModel | ResultSetOf<RestModel> | undefined> {const adapter = this.adapterFor(type);return adapter.find(this, type, findArgs, opts).then((result) => {let hydrated = this._hydrateFindResults(result, type, findArgs, opts);diff --git a/frontend/discourse/type-tests/store/model-name-test.ts b/frontend/discourse/type-tests/store/model-name-test.tsnew file mode 100644index 00000000000..bc5eaf8c0ea--- /dev/null+++ b/frontend/discourse/type-tests/store/model-name-test.ts@@ -0,0 +1,332 @@+import { expectTypeOf } from "expect-type";+import RestModel from "discourse/models/rest";+import type Topic from "discourse/models/topic";+import type UserDraft from "discourse/models/user-draft";+import type {+ default as StoreService,+ ModelCreateAttrsFor,+ ModelFor,+ NormalizeModelName,+} from "discourse/services/store";++/*+ * The store resolves a model class through "model:" + type, and the resolver+ * normalizes that name. These cases come from running ember-resolver's own+ * string helpers over every type name core and the bundled plugins pass to+ * the store, plus edge cases, so the type must match the runtime exactly.+ */+expectTypeOf<NormalizeModelName<"ai-agent">>().toEqualTypeOf<"ai-agent">();+expectTypeOf<+ NormalizeModelName<"ai-decision-model">+>().toEqualTypeOf<"ai-decision-model">();+expectTypeOf<+ NormalizeModelName<"ai-embedding">+>().toEqualTypeOf<"ai-embedding">();+expectTypeOf<NormalizeModelName<"ai-feature">>().toEqualTypeOf<"ai-feature">();+expectTypeOf<NormalizeModelName<"ai-llm">>().toEqualTypeOf<"ai-llm">();+expectTypeOf<+ NormalizeModelName<"ai-mcp-server">+>().toEqualTypeOf<"ai-mcp-server">();+expectTypeOf<NormalizeModelName<"ai-secret">>().toEqualTypeOf<"ai-secret">();+expectTypeOf<NormalizeModelName<"ai-tool">>().toEqualTypeOf<"ai-tool">();+expectTypeOf<NormalizeModelName<"api-key">>().toEqualTypeOf<"api-key">();+expectTypeOf<+ NormalizeModelName<"badge-grouping">+>().toEqualTypeOf<"badge-grouping">();+expectTypeOf<NormalizeModelName<"bookmark">>().toEqualTypeOf<"bookmark">();+expectTypeOf<NormalizeModelName<"category">>().toEqualTypeOf<"category">();+expectTypeOf<NormalizeModelName<"channel">>().toEqualTypeOf<"channel">();+expectTypeOf<NormalizeModelName<"composer">>().toEqualTypeOf<"composer">();+expectTypeOf<+ NormalizeModelName<"composer-message">+>().toEqualTypeOf<"composer-message">();+expectTypeOf<+ NormalizeModelName<"directoryItem">+>().toEqualTypeOf<"directory-item">();+expectTypeOf<+ NormalizeModelName<"discourse-automation-automation">+>().toEqualTypeOf<"discourse-automation-automation">();+expectTypeOf<+ NormalizeModelName<"discourse-automation-scriptable">+>().toEqualTypeOf<"discourse-automation-scriptable">();+expectTypeOf<+ NormalizeModelName<"discourse-workflows-workflow">+>().toEqualTypeOf<"discourse-workflows-workflow">();+expectTypeOf<+ NormalizeModelName<"email-style">+>().toEqualTypeOf<"email-style">();+expectTypeOf<+ NormalizeModelName<"email-template">+>().toEqualTypeOf<"email-template">();+expectTypeOf<+ NormalizeModelName<"embeddable-host">+>().toEqualTypeOf<"embeddable-host">();+expectTypeOf<NormalizeModelName<"embedding">>().toEqualTypeOf<"embedding">();+expectTypeOf<+ NormalizeModelName<"flagged-post">+>().toEqualTypeOf<"flagged-post">();+expectTypeOf<+ NormalizeModelName<"flagged-topic">+>().toEqualTypeOf<"flagged-topic">();+expectTypeOf<NormalizeModelName<"group">>().toEqualTypeOf<"group">();+expectTypeOf<NormalizeModelName<"listTag">>().toEqualTypeOf<"list-tag">();+expectTypeOf<NormalizeModelName<"live-room">>().toEqualTypeOf<"live-room">();+expectTypeOf<NormalizeModelName<"nav-item">>().toEqualTypeOf<"nav-item">();+expectTypeOf<+ NormalizeModelName<"notification">+>().toEqualTypeOf<"notification">();+expectTypeOf<+ NormalizeModelName<"pending-post">+>().toEqualTypeOf<"pending-post">();+expectTypeOf<NormalizeModelName<"permalink">>().toEqualTypeOf<"permalink">();+expectTypeOf<NormalizeModelName<"plugin">>().toEqualTypeOf<"plugin">();+expectTypeOf<NormalizeModelName<"post">>().toEqualTypeOf<"post">();+expectTypeOf<+ NormalizeModelName<"post-reader">+>().toEqualTypeOf<"post-reader">();+expectTypeOf<NormalizeModelName<"post-reply">>().toEqualTypeOf<"post-reply">();+expectTypeOf<+ NormalizeModelName<"post-reply-history">+>().toEqualTypeOf<"post-reply-history">();+expectTypeOf<NormalizeModelName<"postStream">>().toEqualTypeOf<"post-stream">();+expectTypeOf<+ NormalizeModelName<"published-page">+>().toEqualTypeOf<"published-page">();+expectTypeOf<+ NormalizeModelName<"published_page">+>().toEqualTypeOf<"published-page">();+expectTypeOf<NormalizeModelName<"query">>().toEqualTypeOf<"query">();+expectTypeOf<NormalizeModelName<"reviewable">>().toEqualTypeOf<"reviewable">();+expectTypeOf<+ NormalizeModelName<"reviewable-claimed-topic">+>().toEqualTypeOf<"reviewable-claimed-topic">();+expectTypeOf<+ NormalizeModelName<"reviewable-explanation">+>().toEqualTypeOf<"reviewable-explanation">();+expectTypeOf<+ NormalizeModelName<"reviewable-settings">+>().toEqualTypeOf<"reviewable-settings">();+expectTypeOf<+ NormalizeModelName<"reviewable-topic">+>().toEqualTypeOf<"reviewable-topic">();+expectTypeOf<NormalizeModelName<"rule">>().toEqualTypeOf<"rule">();+expectTypeOf<+ NormalizeModelName<"similar-topic">+>().toEqualTypeOf<"similar-topic">();+expectTypeOf<NormalizeModelName<"site">>().toEqualTypeOf<"site">();+expectTypeOf<NormalizeModelName<"site-text">>().toEqualTypeOf<"site-text">();+expectTypeOf<+ NormalizeModelName<"staff-action-log">+>().toEqualTypeOf<"staff-action-log">();+expectTypeOf<NormalizeModelName<"tag">>().toEqualTypeOf<"tag">();+expectTypeOf<NormalizeModelName<"tag-info">>().toEqualTypeOf<"tag-info">();+expectTypeOf<+ NormalizeModelName<"tag-settings">+>().toEqualTypeOf<"tag-settings">();+expectTypeOf<NormalizeModelName<"tagGroup">>().toEqualTypeOf<"tag-group">();+expectTypeOf<+ NormalizeModelName<"tagNotification">+>().toEqualTypeOf<"tag-notification">();+expectTypeOf<NormalizeModelName<"theme">>().toEqualTypeOf<"theme">();+expectTypeOf<+ NormalizeModelName<"tl3Requirements">+>().toEqualTypeOf<"tl3-requirements">();+expectTypeOf<NormalizeModelName<"topic">>().toEqualTypeOf<"topic">();+expectTypeOf<+ NormalizeModelName<"topicDetails">+>().toEqualTypeOf<"topic-details">();+expectTypeOf<NormalizeModelName<"topicList">>().toEqualTypeOf<"topic-list">();+expectTypeOf<NormalizeModelName<"user">>().toEqualTypeOf<"user">();+expectTypeOf<NormalizeModelName<"user-field">>().toEqualTypeOf<"user-field">();+expectTypeOf<NormalizeModelName<"user-note">>().toEqualTypeOf<"user-note">();+expectTypeOf<NormalizeModelName<"web-hook">>().toEqualTypeOf<"web-hook">();+expectTypeOf<+ NormalizeModelName<"web-hook-event">+>().toEqualTypeOf<"web-hook-event">();+expectTypeOf<NormalizeModelName<"APIKey">>().toEqualTypeOf<"apikey">();+expectTypeOf<NormalizeModelName<"postIDs">>().toEqualTypeOf<"post-ids">();+expectTypeOf<NormalizeModelName<"a.B">>().toEqualTypeOf<"a/b">();+expectTypeOf<NormalizeModelName<"_Foo">>().toEqualTypeOf<"-foo">();+expectTypeOf<NormalizeModelName<"Topic">>().toEqualTypeOf<"topic">();+expectTypeOf<+ NormalizeModelName<"user_API_key">+>().toEqualTypeOf<"user-api-key">();+expectTypeOf<NormalizeModelName<"foo bar">>().toEqualTypeOf<"foo-bar">();+expectTypeOf<NormalizeModelName<"x2Y">>().toEqualTypeOf<"x2-y">();+expectTypeOf<+ NormalizeModelName<"already-dashed">+>().toEqualTypeOf<"already-dashed">();+expectTypeOf<NormalizeModelName<"ABC">>().toEqualTypeOf<"abc">();+expectTypeOf<NormalizeModelName<"aBC">>().toEqualTypeOf<"a-bc">();+expectTypeOf<+ NormalizeModelName<"post.reply.history">+>().toEqualTypeOf<"post/reply/history">();+expectTypeOf<NormalizeModelName<"a__b">>().toEqualTypeOf<"a--b">();+expectTypeOf<+ NormalizeModelName<"camelCaseKey">+>().toEqualTypeOf<"camel-case-key">();++class NameTestCamel extends RestModel {+ declare camelField: string;+}++class NameTestDashed extends RestModel {+ declare dashedField: number;+}++class NameTestCollideA extends RestModel {+ declare collideA: true;+}++class NameTestCollideB extends RestModel {+ declare collideB: true;+}++declare module "discourse/services/store" {+ interface ModelRegistry {+ // registered camelCase, as some callers spell it+ nameTestCamel: NameTestCamel;+ // registered dasherized+ "name-test-dashed": NameTestDashed;+ // two spellings of one runtime name+ nameTestCollide: NameTestCollideA;+ "name-test-collide": NameTestCollideB;+ }++ interface ModelCreateAttrsRegistry {+ nameTestCamel: { camelField?: string | number };+ }+}++declare const store: StoreService;+declare const dynamicType: string;++// A camelCase registration is found by every spelling that resolves to it.+expectTypeOf<ModelFor<"nameTestCamel">>().toEqualTypeOf<NameTestCamel>();+expectTypeOf<ModelFor<"name-test-camel">>().toEqualTypeOf<NameTestCamel>();+expectTypeOf<ModelFor<"name_test_camel">>().toEqualTypeOf<NameTestCamel>();++// A dasherized registration is found by camelCase and underscored lookups.+expectTypeOf<ModelFor<"name-test-dashed">>().toEqualTypeOf<NameTestDashed>();+expectTypeOf<ModelFor<"nameTestDashed">>().toEqualTypeOf<NameTestDashed>();+expectTypeOf<ModelFor<"name_test_dashed">>().toEqualTypeOf<NameTestDashed>();++// Two registrations of the same runtime name surface as a union.+expectTypeOf<ModelFor<"name-test-collide">>().toEqualTypeOf<+ NameTestCollideA | NameTestCollideB+>();+expectTypeOf<ModelFor<"nameTestCollide">>().toEqualTypeOf<+ NameTestCollideA | NameTestCollideB+>();++// Core registrations resolve through other spellings too.+expectTypeOf<ModelFor<"user_draft">>().toEqualTypeOf<UserDraft>();+expectTypeOf<ModelFor<"userDraft">>().toEqualTypeOf<UserDraft>();+expectTypeOf<ModelFor<"Topic">>().toEqualTypeOf<Topic>();++// Anything not provably registered falls back to the base model.+expectTypeOf<ModelFor<string>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<`name-test-${string}`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<"name-test-unregistered">>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<"nameTestCamelX">>().toEqualTypeOf<RestModel>();++// A union of names gives the union of their models.+expectTypeOf<ModelFor<"nameTestCamel" | "name-test-dashed">>().toEqualTypeOf<+ NameTestCamel | NameTestDashed+>();++// Creation attributes follow the same normalization.+expectTypeOf<ModelCreateAttrsFor<"name-test-camel">>().toEqualTypeOf<{+ camelField?: string | number;+}>();++// The store methods pick the model up through any spelling.+expectTypeOf(+ store.createRecord("name-test-camel")+).toEqualTypeOf<NameTestCamel>();+expectTypeOf(+ store.createRecord("name_test_camel", { camelField: 1 })+).toEqualTypeOf<NameTestCamel>();+// @ts-expect-error the registered creation attributes are still checked+store.createRecord("name-test-camel", { camelField: true });+expectTypeOf(+ store.find("nameTestDashed", 1)+).resolves.toEqualTypeOf<NameTestDashed>();+expectTypeOf(store.createRecord(dynamicType)).toEqualTypeOf<RestModel>();++/* More cases generated with the installed resolver's string helpers. */+type NormalizationCases = [+ ["", ""],+ ["aBaB", "a-ba-b"],+ ["a_ B", "a--b"],+ ["foo\tBar\nBaz", "foo\tbar\nbaz"],+ ["éX", "éx"],+ ["aÉ", "aé"],+ ["İTest", "i̇test"],+ ["ΟΣ", "ος"],+ ["under_score.path Name", "under-score/path-name"],+ ["foo:bar:baz", "foo"],+ [+ "aBcDeFgHiJaBcDeFgHiJaBcDeFgHiJaBcDeFgHiJaBcDeFgHiJaBcDeFgHiJ",+ "a-bc-de-fg-hi-ja-bc-de-fg-hi-ja-bc-de-fg-hi-ja-bc-de-fg-hi-ja-bc-de-fg-hi-ja-bc-de-fg-hi-j",+ ],+];++expectTypeOf<{+ [K in NormalizationCases[number] as K[0]]: NormalizeModelName<K[0]>;+}>().toEqualTypeOf<{+ [K in NormalizationCases[number] as K[0]]: K[1];+}>();++declare module "discourse/services/store" {+ interface ModelRegistry {+ extra_under_score: UserDraft;+ "extra.path Name": Topic;+ }++ interface ModelCreateAttrsRegistry {+ extra_under_score: { source?: "underscore" };+ extraUnderScore: { source?: "camel" };+ }+}++expectTypeOf<ModelFor<"extraUnderScore">>().toEqualTypeOf<UserDraft>();+expectTypeOf<ModelFor<"extra/path-name">>().toEqualTypeOf<Topic>();+expectTypeOf<ModelCreateAttrsFor<"extra-under-score">>().toEqualTypeOf<+ { source?: "underscore" } | { source?: "camel" }+>();+expectTypeOf<ModelCreateAttrsFor<"extraUnderScore">>().toEqualTypeOf<+ { source?: "underscore" } | { source?: "camel" }+>();+expectTypeOf<ModelCreateAttrsFor<"extra_under_score">>().toEqualTypeOf<+ { source?: "underscore" } | { source?: "camel" }+>();+expectTypeOf<ModelCreateAttrsFor<"userDraft">>().toEqualTypeOf<+ ModelCreateAttrsFor<"user-draft">+>();+expectTypeOf<+ ModelCreateAttrsFor<"extra/path-name" | "extra-under-score">+>().toEqualTypeOf<+ | ModelCreateAttrsFor<"extra.path Name">+ | { source?: "underscore" }+ | { source?: "camel" }+>();++expectTypeOf<NormalizeModelName<string>>().toEqualTypeOf<string>();+expectTypeOf<NormalizeModelName<`topic${string}`>>().toEqualTypeOf<string>();+expectTypeOf<NormalizeModelName<`topic${number}`>>().toEqualTypeOf<string>();+expectTypeOf<NormalizeModelName<Uppercase<string>>>().toEqualTypeOf<string>();+expectTypeOf<NormalizeModelName<"userDraft" | "Topic">>().toEqualTypeOf<+ "user-draft" | "topic"+>();+expectTypeOf<NormalizeModelName<never>>().toBeNever();+expectTypeOf<ModelFor<never>>().toBeNever();+expectTypeOf<ModelFor<`topic${string}`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<`${string}topic`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<`to${string}pic`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<`topic${number}`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<`topic:${string}`>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelFor<Uppercase<string>>>().toEqualTypeOf<RestModel>();+expectTypeOf<ModelCreateAttrsFor<string>>().toEqualTypeOf<+ ModelCreateAttrsFor<"extra-unregistered">+>();