Skip to content

Commit

Permalink
feat: 🎸 implement info() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 20, 2023
1 parent 9a7fd37 commit eea9215
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/crud/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface CrudPutOptions {

export interface CrudTypeEntry {
/** Kind of the resource, type or item. */
kind: 'type' | 'item';
type: 'resource' | 'collection';
/** Name of the resource. */
id: string;
}
Expand All @@ -94,5 +94,5 @@ export interface CrudScanResult {

export interface CrudScanEntry extends CrudTypeEntry {
/** Collection, which contains this entry. */
type: CrudCollection;
collection: CrudCollection;
}
23 changes: 20 additions & 3 deletions src/fsa-crud/FsaCrud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ export class FsaCrud implements crud.CrudApi {
};

public readonly del = async (collection: crud.CrudCollection, id: string, silent?: boolean): Promise<void> => {
assertType(collection, 'get', 'crudfs');
assertName(id, 'get', 'crudfs');
assertType(collection, 'del', 'crudfs');
assertName(id, 'del', 'crudfs');
try {
const [dir] = await this.getFile(collection, id);
await dir.removeEntry(id, {recursive: false});
Expand All @@ -86,7 +86,24 @@ export class FsaCrud implements crud.CrudApi {
};

public readonly info = async (collection: crud.CrudCollection, id?: string): Promise<crud.CrudResourceInfo> => {
throw new Error('Not implemented');
assertType(collection, 'info', 'crudfs');
if (id) {
assertName(id, 'info', 'crudfs');
const [, file] = await this.getFile(collection, id);
const blob = await file.getFile();
return {
type: 'resource',
id,
size: blob.size,
modified: blob.lastModified,
};
} else {
await this.getDir(collection, false);
return {
type: 'collection',
id: '',
};
}
};

public readonly drop = async (collection: crud.CrudCollection): Promise<void> => {
Expand Down
68 changes: 63 additions & 5 deletions src/fsa-crud/__tests__/FsaCrud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ onlyOnNode20('FsaCrud', () => {
describe('.put()', () => {
test('throws if the type is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.put(['', 'foo'], 'bar', new Uint8Array()));
const [, err] = await of(crud.put(['.', 'foo'], 'bar', new Uint8Array()));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'put' on 'crudfs': Name is not allowed.");
});

test('throws if id is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.put(['foo'], '', new Uint8Array()));
const [, err] = await of(crud.put(['foo'], '..', new Uint8Array()));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'put' on 'crudfs': Name is not allowed.");
});

test('can store a resource at root', async () => {
Expand Down Expand Up @@ -81,12 +83,14 @@ onlyOnNode20('FsaCrud', () => {
const {crud} = setup();
const [, err] = await of(crud.get(['', 'foo'], 'bar'));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'get' on 'crudfs': Name is not allowed.");
});

test('throws if id is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.get(['foo'], ''));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'get' on 'crudfs': Name is not allowed.");
});

test('throws if collection does not exist', async () => {
Expand Down Expand Up @@ -115,17 +119,18 @@ onlyOnNode20('FsaCrud', () => {
describe('.del()', () => {
test('throws if the type is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.del(['', 'foo'], 'bar'));
const [, err] = await of(crud.del(['sdf\\dd', 'foo'], 'bar'));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'del' on 'crudfs': Name is not allowed.");
});

test('throws if id is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.del(['foo'], ''));
const [, err] = await of(crud.del(['foo'], 'asdf/asdf'));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'del' on 'crudfs': Name is not allowed.");
});


describe('when collection does not exist', () => {
test('throws by default', async () => {
const {crud} = setup();
Expand Down Expand Up @@ -166,4 +171,57 @@ onlyOnNode20('FsaCrud', () => {
expect((<any>err).name).toBe('ResourceNotFound');
});
});

describe('.info()', () => {
test('throws if the type is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.info(['', 'foo'], 'bar'));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'info' on 'crudfs': Name is not allowed.");
});

test('throws if id is not valid', async () => {
const {crud} = setup();
const [, err] = await of(crud.info(['foo'], '/'));
expect(err).toBeInstanceOf(TypeError);
expect((<any>err).message).toBe("Failed to execute 'info' on 'crudfs': Name is not allowed.");
});

test('can retrieve information about a resource', async () => {
const {crud} = setup();
await crud.put(['foo'], 'bar', b('abc'));
const info = await crud.info(['foo'], 'bar');
expect(info).toMatchObject({
type: 'resource',
id: 'bar',
size: 3,
modified: expect.any(Number),
});
});

test('can retrieve information about a collection', async () => {
const {crud} = setup();
await crud.put(['foo'], 'bar', b('abc'));
const info = await crud.info(['foo']);
expect(info).toMatchObject({
type: 'collection',
});
});

test('throws when resource not found', async () => {
const {crud} = setup();
await crud.put(['foo'], 'bar', b('abc'));
const [, err] = await of(crud.info(['foo'], 'baz'));
expect(err).toBeInstanceOf(DOMException);
expect((<any>err).name).toBe('ResourceNotFound');
});

test('throws when collection not found', async () => {
const {crud} = setup();
await crud.put(['foo', 'a'], 'bar', b('abc'));
const [, err] = await of(crud.info(['foo', 'b'], 'baz'));
expect(err).toBeInstanceOf(DOMException);
expect((<any>err).name).toBe('CollectionNotFound');
});
});
});

0 comments on commit eea9215

Please sign in to comment.