API 參考資料

下麵包含 Bundle Builder API 規範的概述,包括 TypeScript 定義和詳細描述。

捆綁文件介面

配置集合中單一文件的規格:

type BundleDocument = {
  // A list of document IDs to serve in the bundle.
  docs?: Array<string>;
  // A map containing individual named queries and their definitions.
  queries?: Map<string, QueryDefinition[]>;
  // A map of parameters and their definitions, which can be provided to a query definition.
  params?: Map<string, ParamDefinition>;
  // Specifies how long to keep the bundle in the client's cache, in seconds. If not defined, client-side cache is disabled.
  clientCache?: string;
  // Only used in combination with Firebase Hosting. Specifies how long to keep the bundle in Firebase Hosting CDN cache, in seconds.
  serverCache: string;
  // Specifies how long (in seconds) to keep the bundle in a Cloud Storage bucket, in seconds. If not defined, Cloud Storage bucket is not accessed.
  fileCache?: string;
  // If a 'File Cache' is specified, bundles created before this timestamp will not be file cached.
  notBefore?: Timestamp;
};

參數定義介面

BundleDocument中定義的單一參數的規格。

type ParamDefinition = {
  // Whether this parameter is required. If not provided as a query string, an error will be thrown.
  required: boolean;
  // The type of value which will be parsed, defaults to 'string'.
  type?:
    | "string"
    | "integer"
    | "float"
    | "boolean"
    | "string-array"
    | "integer-array"
    | "float-array";
};

例如,給定以下參數:

params: {
  name: {
    required: true,
    type: 'string',
  }
}

當向捆綁 HTTP 端點發出請求時,可以透過查詢參數提供該參數,例如?name=david 。此參數可以在QueryDefinition (見下文)值 ( $name ) 中使用來動態建立捆綁包。

查詢定義介面

查詢定義用於在套件上建立命名查詢。 queries映射中的每個物件都將建立一個新的命名查詢,使用物件鍵作為名稱。每個查詢必須指定一個集合,以及可選的要執行的查詢條件清單。

type QueryDefinition = {
  // The collection to perform the query on.
  collection: string;
  // An optional list of conditions to perform on the specified collection.
  conditions?: QueryCondition[];
};

conditions參數可以包含QueryCondition介面的陣列。數組中的每個項目只能包含一個條件。

type QueryCondition = {
  // Performs a `where` filter on the collection on a given FieldPath, operator and value.
  where?: [
    string,
    (
      | "<"
      | "<="
      | "=="
      | ">="
      | ">"
      | "!="
      | "array-contains"
      | "in"
      | "not-in"
      | "array-contains-any"
    ),
    any
  ];
  orderBy?: [string, ("asc" | "desc")?];
  limit?: number;
  limitToLast?: number;
  offset?: number;
  startAt?: string;
  startAfter?: string;
  endAt?: string;
  endBefore?: string;
};

例如,要使用 where 和 limit 條件在products集合上建立名為「products」的查詢,資料結構輸出應與下列內容相符:

queries: {
  products: {
    collection: 'products',
    conditions: [
      { where: ['type', '==', 'featured'] },
      { limit: 10 },
    ],
  }
}

當您向innot-inarray-contains-any過濾器提供陣列值時,您必須提供逗號分隔值,因為 Firestore 不支援巢狀數組值。例如:

{ where: ['category', 'in', 'womens,shorts'] }, // ['womens', 'shorts']

任何數字值都將被解析為數字,但是如果需要字串數字值,則應將其括在括號中:

{ where: ['price', 'in', '1,2.5'] }, // [1, 2.5]
{ where: ['price', 'in', '"1","2.5"'] }, // ['1', '2.5']

條件也可以與參數一起使用。例如,如果定義了參數type (見上文),則可以將其提供給條件值以透過$語法提供動態資料包:

// ?type=featured


    conditions: [
      { where: ['type', '==', '$type'] },