Skip to content

Commit

Permalink
feat: fileCollectionFromFileArray as optional baseURL in options
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed Dec 15, 2022
1 parent c1c89fb commit c452721
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
27 changes: 19 additions & 8 deletions src/fileCollectionFromFileArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export async function fileCollectionFromFileArray(
lastModified: number;
size: number;
}[],
baseURL?: string | URL,
options: ExpandOptions & FilterOptions = {},

options: { baseURL?: string | URL } & ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
const { baseURL } = options;
let fileCollectionItems: FileCollectionItem[] = [];
/*
Answer should contain:
Expand All @@ -47,14 +48,24 @@ export async function fileCollectionFromFileArray(
relativePath: entry.relativePath,
lastModified: entry.lastModified,
text: async (): Promise<string> => {
const fileURL = new URL(entry.relativePath, baseURL).href;
const response = await fetch(fileURL);
return response.text();
if (baseURL) {
const fileURL = new URL(entry.relativePath, baseURL);
const response = await fetch(fileURL);
return response.text();
} else {
const response = await fetch(entry.relativePath);
return response.text();
}
},
arrayBuffer: async (): Promise<ArrayBuffer> => {
const fileURL = new URL(entry.relativePath, baseURL).href;
const response = await fetch(fileURL);
return response.arrayBuffer();
if (baseURL) {
const fileURL = new URL(entry.relativePath, baseURL);
const response = await fetch(fileURL);
return response.arrayBuffer();
} else {
const response = await fetch(entry.relativePath);
return response.arrayBuffer();
}
},
stream: (): ReadableStream => {
throw new Error('stream not yet implemented');
Expand Down
2 changes: 1 addition & 1 deletion src/fileCollectionFromWebservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ export async function fileCollectionFromWebservice(
const entries = await response.json();
const baseURL = url;

return fileCollectionFromFileArray(entries, baseURL, options);
return fileCollectionFromFileArray(entries, { baseURL, ...options });
}

0 comments on commit c452721

Please sign in to comment.