Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fileCollectionFromFileArray #64

Merged
merged 1 commit into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/fileCollectionFromFileArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fetch from 'cross-fetch';

import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';

/**
* Creates a FileCollection from a webservice. This webservice should return an array of objects containing the properties:
* - relativePath
* - name
* - lastModified
* - size
* By default this method will expand all zip and gzip files
* @param url
* @param options
* @returns
*/
export async function fileCollectionFromFileArray(
entries: {
relativePath: string;
name: string;
lastModified: number;
size: number;
}[],
baseURL: string | URL,
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
let fileCollectionItems: FileCollectionItem[] = [];
/*
Answer should contain:
- relativePath
- name
- lastModified
- size
And we need to add those async functions:
- stream
- text
- arrayBuffer
*/
for (const entry of entries) {
fileCollectionItems.push({
name: entry.name,
size: entry.size,
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();
},
arrayBuffer: async (): Promise<ArrayBuffer> => {
const fileURL = new URL(entry.relativePath, baseURL).href;
const response = await fetch(fileURL);
return response.arrayBuffer();
},
stream: (): ReadableStream => {
throw new Error('stream not yet implemented');
},
});
}
fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);
return new FileCollection(fileCollectionItems);
}
45 changes: 5 additions & 40 deletions src/fileCollectionFromWebservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import fetch from 'cross-fetch';

import { ExpandOptions } from './ExpandOptions';
import { FileCollection } from './FileCollection';
import { FileCollectionItem } from './FileCollectionItem';
import { maybeExpand } from './utilities/maybeExpand';
import { FilterOptions, maybeFilter } from './utilities/maybeFilter';
import { fileCollectionFromFileArray } from './fileCollectionFromFileArray';
import { FilterOptions } from './utilities/maybeFilter';

/**
* Creates a FileCollection from a webservice. This webservice should return an array of objects containing the properties:
Expand All @@ -22,42 +21,8 @@ export async function fileCollectionFromWebservice(
options: ExpandOptions & FilterOptions = {},
): Promise<FileCollection> {
const response = await fetch(url.toString());
const baseURL = url;
const entries = await response.json();
let fileCollectionItems: FileCollectionItem[] = [];
/*
Answer should contain:
- relativePath
- name
- lastModified
- size
And we need to add those async functions:
- stream
- text
- arrayBuffer
*/
for (const entry of entries) {
fileCollectionItems.push({
name: entry.name,
size: entry.size,
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();
},
arrayBuffer: async (): Promise<ArrayBuffer> => {
const fileURL = new URL(entry.relativePath, baseURL).href;
const response = await fetch(fileURL);
return response.arrayBuffer();
},
stream: (): ReadableStream => {
throw new Error('stream not yet implemented');
},
});
}
fileCollectionItems = await maybeExpand(fileCollectionItems, options);
fileCollectionItems = await maybeFilter(fileCollectionItems, options);
return new FileCollection(fileCollectionItems);
const baseURL = url;

return fileCollectionFromFileArray(entries, baseURL, options);
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './fileCollectionFromPaths';
export * from './fileCollectionFromZip';
export * from './fileCollectionFromWebservice';
export * from './fileCollectionFromFileList';
export * from './fileCollectionFromFileArray';
export * from './fileCollectionFromFiles';
export * from './groupFiles';
export * from './FileCollection';
Expand Down