Skip to content

Commit

Permalink
Add query params to listFiles to search docs by content
Browse files Browse the repository at this point in the history
  • Loading branch information
redneckz committed Apr 11, 2024
1 parent 174a3b4 commit a555fe1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 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.6",
"version": "0.0.8",
"license": "MIT",
"author": {
"name": "redneckz",
Expand Down
4 changes: 2 additions & 2 deletions src/ContentPageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type JSONNode, type JSONRecord } from '@redneckz/json-op';
import path from 'path/posix';
import type { FileAPI, FilePath } from './api/FileAPI';
import type { FileAPI, FilePath, FileQuery } from './api/FileAPI';
import { FileStorageAPI, type FileStorageOptions } from './api/FileStorageAPI';
import { FileSystemAPI } from './api/FileSystemAPI';
import { isFileExists } from './fs/isFileExists';
Expand Down Expand Up @@ -31,7 +31,7 @@ export class ContentPageRepository implements FileAPI {
private readonly storageAPI = new FileStorageAPI(options)
) {}

async listFiles(options: { dir?: string; ext?: string }): Promise<FilePath[]> {
async listFiles(options: { dir?: string; ext?: string } & FileQuery): Promise<FilePath[]> {
return (
await Promise.allSettled([FileSystemAPI.inst.listFiles(options), this.storageAPI.listFiles(options)])
).flatMap(result => (result.status === 'fulfilled' ? result.value : []));
Expand Down
3 changes: 2 additions & 1 deletion src/api/FileAPI.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { type JSONNode } from '@redneckz/json-op';

export type FilePath = string;
export type FileQuery = Record<string, string>;

export interface FileAPI {
listFiles(options: { dir?: string; ext?: string }): Promise<FilePath[]>;
listFiles(options: { dir?: string; ext?: string } & FileQuery): Promise<FilePath[]>;
readJSON<T extends JSONNode = JSONNode>(filePath: FilePath): Promise<T>;
}
16 changes: 10 additions & 6 deletions src/api/FileStorageAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type JSONNode } from '@redneckz/json-op';
import type { FileAPI, FilePath } from './FileAPI';
import type { FileAPI, FilePath, FileQuery } from './FileAPI';

const API_BASE_PATH = '/api/v1/wcms-file-storage';

Expand All @@ -26,16 +26,16 @@ export class FileStorageAPI implements FileAPI {

constructor(private readonly options: FileStorageOptions = {}) {}

async listFiles(options: { dir?: string; ext?: string }): Promise<FilePath[]> {
async listFiles(options: { dir?: string; ext?: string } & FileQuery): Promise<FilePath[]> {
if (!this.projectId) {
return [];
}

let pageItems: FileMeta[] = [];
let items: FileMeta[] = [];
do {
const maxRevision = pageItems.length ? pageItems[pageItems.length - 1].revision : 0;
pageItems = await this.fetchProjectDocs(options, maxRevision);
const prevRevision = pageItems.length ? pageItems[pageItems.length - 1].revision : 0;
pageItems = await this.fetchProjectDocs(options, prevRevision);
items = items.concat(pageItems);
} while (pageItems.length > 0);
return items.map(({ publicId, name }) => name || publicId);
Expand All @@ -52,11 +52,15 @@ export class FileStorageAPI implements FileAPI {
return (await response.json()) as T;
}

private async fetchProjectDocs({ dir, ext }: { dir?: string; ext?: string } = {}, from = 0): Promise<FileMeta[]> {
private async fetchProjectDocs(
{ dir, ext, ...query }: { dir?: string; ext?: string } & FileQuery = {},
fromRevision = 0
): Promise<FileMeta[]> {
const params = new URLSearchParams({
...(dir ? { dir } : {}),
...(ext ? { ext } : {}),
from: String(from ?? 0)
from: String(fromRevision ?? 0),
...query
});
const response = await (this.options.fetch ?? globalThis.fetch)(
`${this.baseURL}${API_BASE_PATH}/project/${this.projectId}/doc?${params}`
Expand Down

0 comments on commit a555fe1

Please sign in to comment.