Skip to content

Commit

Permalink
Add utility composeFileReaderAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
redneckz committed Apr 16, 2024
1 parent 6d14cf7 commit 37fd1ad
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redneckz/wildless-cms-content",
"version": "0.0.11",
"version": "0.0.13",
"license": "MIT",
"author": {
"name": "redneckz",
Expand Down
6 changes: 5 additions & 1 deletion src/api/FileFetcherAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ export class FileFetcherAPI implements FileReaderAPI {

async readJSON<T extends JSONNode = JSONNode>(filePath: string): Promise<T> {
const response = await fetch(`${this.baseURL}/${normalizeFilePath(filePath)}`);
return response.json();
if (response.status >= 200 && response.status < 300) {
return response.json();
} else {
throw new Error(`Failed to read "${filePath}"`);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/api/FileStorageAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ export class FileStorageAPI implements FileAPI {
const response = await (this.options.fetch ?? globalThis.fetch)(
`${this.baseURL}${API_BASE_PATH}/projects/${this.projectId}/files/${encodeURIComponent(filePath)}`
);
return (await response.json()) as T;
if (response.status >= 200 && response.status < 300) {
return response.json();
} else {
throw new Error(`Failed to read "${filePath}"`);
}
}

private async fetchAllProjectDocs(options: ListFilesOptions = {}): Promise<FileMetaWithJSON[]> {
Expand Down
4 changes: 3 additions & 1 deletion src/api/FileSystemAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const find = promisify(glob);
export class FileSystemAPI implements FileAPI {
public static readonly inst: FileAPI = new FileSystemAPI();

constructor(private readonly basePath?: string) {}

async listFiles({ dir, ext, size }: ListFilesOptions): Promise<FilePath[]> {
const files: string[] = await find(`*${ext}`, { cwd: dir, matchBase: true });
return files.slice(0, size).map(_ => (dir ? path.join(dir, _) : _));
Expand All @@ -26,6 +28,6 @@ export class FileSystemAPI implements FileAPI {
}

async readJSON<T extends JSONNode = JSONNode>(filePath: FilePath): Promise<T> {
return readJSON(filePath);
return readJSON(this.basePath ? path.join(this.basePath, filePath) : filePath);
}
}
11 changes: 11 additions & 0 deletions src/api/composeFileReaderAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type FileReaderAPI } from './FileAPI';

export const composeFileReaderAPI = (primary: FileReaderAPI, secondary: FileReaderAPI): FileReaderAPI => ({
async readJSON(filePath) {
try {
return await primary.readJSON(filePath);
} catch (ex) {
return secondary.readJSON(filePath);
}
}
});
1 change: 1 addition & 0 deletions src/index.edge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from './api/FileAPI';
export * from './api/FileFetcherAPI';
export * from './api/FileStorageAPI';
export * from './api/composeFileReaderAPI';

export * from './inheritance/index';
export * from './normalizePageData/index';
Expand Down
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 './api/FileAPI';
export * from './api/FileFetcherAPI';
export * from './api/FileStorageAPI';
export * from './api/FileSystemAPI';
export * from './api/composeFileReaderAPI';

export { computeAPIFallback } from './computeAPIFallback';

Expand Down

0 comments on commit 37fd1ad

Please sign in to comment.