以下概略說明瞭 Bundle Builder API 的規格,包括 TypeScript 定義和詳細說明。
BundleDocument 介面
已設定集合中單一文件的規格:
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;
};
ParamDefinition 介面
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
) 中,用來動態建立套裝組合。
QueryDefinition 介面
查詢定義可用於在軟體包上建立命名查詢。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;
};
舉例來說,如要在 products
集合上建立名為「products」的查詢,並設定 where 和 limit 限制條件,資料結構輸出內容應符合下列條件:
queries: {
products: {
collection: 'products',
conditions: [
{ where: ['type', '==', 'featured'] },
{ limit: 10 },
],
}
}
為 in
、not-in
或 array-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'] },