From 631046147845f36b8fd52158246db78cfae95a29 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:21:07 -0700 Subject: [PATCH] feat(asset): add headers to StoreArgs (#928) * add headers to StoreArgs * fix --- packages/assets/src/canisters/assets_idl.js | 3 ++- packages/assets/src/canisters/assets_service.ts | 1 + packages/assets/src/index.ts | 8 +++++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/assets/src/canisters/assets_idl.js b/packages/assets/src/canisters/assets_idl.js index 4bf539156..e27f3a7d9 100644 --- a/packages/assets/src/canisters/assets_idl.js +++ b/packages/assets/src/canisters/assets_idl.js @@ -2,9 +2,11 @@ export const idlFactory = ({ IDL }) => { const ClearArguments = IDL.Record({}); const BatchId = IDL.Nat; const Key = IDL.Text; + const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); const CreateAssetArguments = IDL.Record({ key: Key, content_type: IDL.Text, + headers: IDL.Opt(IDL.Vec(HeaderField)), }); const UnsetAssetContentArguments = IDL.Record({ key: Key, @@ -25,7 +27,6 @@ export const idlFactory = ({ IDL }) => { SetAssetContent: SetAssetContentArguments, Clear: ClearArguments, }); - const HeaderField = IDL.Tuple(IDL.Text, IDL.Text); const HttpRequest = IDL.Record({ url: IDL.Text, method: IDL.Text, diff --git a/packages/assets/src/canisters/assets_service.ts b/packages/assets/src/canisters/assets_service.ts index c3cbc9bbd..ee5aee405 100644 --- a/packages/assets/src/canisters/assets_service.ts +++ b/packages/assets/src/canisters/assets_service.ts @@ -13,6 +13,7 @@ export type ClearArguments = Record; export interface CreateAssetArguments { key: Key; content_type: string; + headers: [] | [Array]; } export interface DeleteAssetArguments { diff --git a/packages/assets/src/index.ts b/packages/assets/src/index.ts index 73e6fd273..a2a2579de 100644 --- a/packages/assets/src/index.ts +++ b/packages/assets/src/index.ts @@ -60,6 +60,11 @@ export interface StoreConfig { * @default File/Blob object type or type from file name extension */ contentType?: string; + /** + * Custom headers to be sent with the asset + * @default [] + */ + headers?: Array<[string, string]>; /** * Content encoding * @default 'identity' @@ -330,9 +335,10 @@ class AssetManagerBatch { }), ); await readable.close(); + const headers: [] | [[string,string][]] = config?.headers ? [config.headers] : []; return [ { - CreateAsset: { key, content_type: config?.contentType ?? readable.contentType }, + CreateAsset: { key, content_type: config?.contentType ?? readable.contentType, headers }, }, { SetAssetContent: {