Skip to content

Commit a148fb8

Browse files
committed
feat: 🎸 implement .scan() method for FSA CRUD
1 parent 921e05d commit a148fb8

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

‎src/crud/__tests__/testCrudfs.ts

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { of } from '../../thingies';
2-
import type { CrudApi } from '../types';
2+
import type { CrudApi, CrudCollectionEntry } from '../types';
33

44
export type Setup = () => {
55
crud: CrudApi;
@@ -271,12 +271,59 @@ export const testCrudfs = (setup: Setup) => {
271271
});
272272
});
273273

274+
describe('.scan()', () => {
275+
test('throws if the collection is not valid', async () => {
276+
const { crud } = setup();
277+
try {
278+
const iterable = crud.scan(['./..', 'foo']);
279+
await iterable.next();
280+
throw 'should not reach here';
281+
} catch (err) {
282+
expect(err).toBeInstanceOf(TypeError);
283+
expect((<any>err).message).toBe("Failed to execute 'scan' on 'crudfs': Name is not allowed.");
284+
}
285+
});
286+
287+
test('can retrieve a list of resources and collections at root', async () => {
288+
const { crud } = setup();
289+
await crud.put(['foo'], 'bar', b('1'));
290+
await crud.put([], 'baz', b('1'));
291+
await crud.put([], 'qux', b('2'));
292+
const list: CrudCollectionEntry[] = [];
293+
for await (const entry of crud.scan([])) list.push(entry);
294+
expect(list.length).toBe(3);
295+
expect(list.find(x => x.id === 'baz')).toMatchObject({
296+
type: 'resource',
297+
id: 'baz',
298+
});
299+
expect(list.find(x => x.id === 'qux')).toMatchObject({
300+
type: 'resource',
301+
id: 'qux',
302+
});
303+
expect(list.find(x => x.id === 'foo')).toMatchObject({
304+
type: 'collection',
305+
id: 'foo',
306+
});
307+
});
308+
309+
test('throws when trying to list a non-existing collection', async () => {
310+
const { crud } = setup();
311+
await crud.put(['foo'], 'bar', b('1'));
312+
await crud.put([], 'baz', b('1'));
313+
await crud.put([], 'qux', b('2'));
314+
const iterator = crud.scan(['gg']);
315+
const [, err] = await of(iterator.next());
316+
expect(err).toBeInstanceOf(DOMException);
317+
expect((<any>err).name).toBe('CollectionNotFound');
318+
});
319+
});
320+
274321
describe('.list()', () => {
275322
test('throws if the collection is not valid', async () => {
276323
const { crud } = setup();
277324
const [, err] = await of(crud.list(['./..', 'foo']));
278325
expect(err).toBeInstanceOf(TypeError);
279-
expect((<any>err).message).toBe("Failed to execute 'drop' on 'crudfs': Name is not allowed.");
326+
expect((<any>err).message).toBe("Failed to execute 'scan' on 'crudfs': Name is not allowed.");
280327
});
281328

282329
test('can retrieve a list of resources and collections at root', async () => {
@@ -300,7 +347,7 @@ export const testCrudfs = (setup: Setup) => {
300347
});
301348
});
302349

303-
test('throws when try to list a non-existing collection', async () => {
350+
test('throws when trying to list a non-existing collection', async () => {
304351
const { crud } = setup();
305352
await crud.put(['foo'], 'bar', b('1'));
306353
await crud.put([], 'baz', b('1'));

‎src/fsa-to-crud/FsaCrud.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,27 @@ export class FsaCrud implements crud.CrudApi {
137137
}
138138
};
139139

140-
public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
141-
assertType(collection, 'drop', 'crudfs');
140+
public readonly scan = async function* (collection: crud.CrudCollection): AsyncIterableIterator<crud.CrudCollectionEntry> {
141+
assertType(collection, 'scan', 'crudfs');
142142
const [dir] = await this.getDir(collection, false);
143-
const entries: crud.CrudCollectionEntry[] = [];
144143
for await (const [id, handle] of dir.entries()) {
145144
if (handle.kind === 'file') {
146-
entries.push({
145+
yield {
147146
type: 'resource',
148147
id,
149-
});
148+
};
150149
} else if (handle.kind === 'directory') {
151-
entries.push({
150+
yield {
152151
type: 'collection',
153152
id,
154-
});
153+
};
155154
}
156155
}
156+
};
157+
158+
public readonly list = async (collection: crud.CrudCollection): Promise<crud.CrudCollectionEntry[]> => {
159+
const entries: crud.CrudCollectionEntry[] = [];
160+
for await (const entry of this.scan(collection)) entries.push(entry);
157161
return entries;
158162
};
159163

0 commit comments

Comments
 (0)