跳到主要内容

Values

与 Array 一样,Values 的大小没有上限。这里的 definition 描述了预期的值类型,键可以是任意字符串。

描述一个其值遵循给定 schema 的 map。

  • definition:必需 该数组所包含的单一 schema,或者 一个从属性值到 schema 的映射。
  • schemaAttribute:可选(当 definition 不是单一 schema 时必需)每个 entity 上的一个属性,根据 definition 映射决定规范化时使用哪个 schema。可以是字符串或函数。如果是函数,它接受以下参数:
    • value:entity 的输入值。
    • parent:输入数组的父对象。
    • key:输入数组在父对象上所处的键。
提示

使用 Collections 使其可变(可以赋值新的条目)

实例方法​

  • define(definition):调用时,传入的 definition 会与传给 Values 构造函数的原始 definition 合并。这个方法通常用于在 schema 中创建循环引用。
命名

Values 以 Object.values() 命名,因为它的 schema 用于描述对象的值。

用法​

Fixtures
GET /items
{"firstThing":{"id":1},"secondThing":{"id":2}}
▶ItemPage.tsx
import { Entity, RestEndpoint, Values } from '@data-client/rest';
import { useSuspense } from '@data-client/react';

export class Item extends Entity {
  id = 0;
}
export const getItems = new RestEndpoint({
  path: '/items',
  schema: new Values(Item),
});
function ItemPage() {
  const items = useSuspense(getItems);
  return <pre>{JSON.stringify(items, undefined, 2)}</pre>;
}
render(<ItemPage />);
结果
Store▶

更新多个 entity​

将 Values 与 Controller.set() 搭配使用,即可在一次 store 更新中写入多个 entity,无需 endpoint。

ctrl.set(getItems.schema, {
firstThing: { id: 1 },
secondThing: { id: 2 },
});

多态类型​

如果输入数据是一个对象,其值包含多种类型的 entity,而它们的 schema 又不容易通过键来确定,你可以使用 schema 映射,就像 Union 和 schema.Array 那样。

备注

如果数据返回了一个你没有为其提供映射的对象,结果中会返回原始对象,并且不会创建 entity。

string schemaAttribute​

Fixtures
GET /feed
[{"id":1,"type":"link","url":"https://ntucker.true.io","title":"Nate site"},{"id":10,"type":"post","content":"good day!"}]
▶api/Feed
import { Entity, RestEndpoint, Values } from '@data-client/rest';

export abstract class FeedItem extends Entity {
  readonly id: number = 0;
  declare readonly type: 'link' | 'post';
}
export class Link extends FeedItem {
  readonly type = 'link' as const;
  readonly url: string = '';
  readonly title: string = '';
}
export class Post extends FeedItem {
  readonly type = 'post' as const;
  readonly content: string = '';
}
export const getFeed = new RestEndpoint({
  path: '/feed',
  schema: new Values(
    {
      link: Link,
      post: Post,
    },
    'type',
  ),
});
▶FeedList
结果
Store▶

function schemaAttribute​

返回值应与 definition 中的某个键相匹配。这里我们展示与 'string' 情形相同的行为,只是会追加一个 's'。

Fixtures
GET /feed
[{"id":1,"type":"link","url":"https://ntucker.true.io","title":"Nate site"},{"id":10,"type":"post","content":"good day!"}]
▶api/Feed
import { Entity, RestEndpoint, Values } from '@data-client/rest';

export abstract class FeedItem extends Entity {
  readonly id: number = 0;
  declare readonly type: 'link' | 'post';
}
export class Link extends FeedItem {
  readonly type = 'link' as const;
  readonly url: string = '';
  readonly title: string = '';
}
export class Post extends FeedItem {
  readonly type = 'post' as const;
  readonly content: string = '';
}
export const getFeed = new RestEndpoint({
  path: '/feed',
  schema: new Values(
    {
      links: Link,
      posts: Post,
    },
    (input: Link | Post, parent: unknown, key: string) => `${input.type}s`,
  ),
});
▶FeedList
结果
Store▶