Skip to content

Commit

Permalink
feat(storage-client): compatibel with new storage server response types
Browse files Browse the repository at this point in the history
  • Loading branch information
AliMD authored and njfamirm committed May 18, 2023
1 parent 44c1086 commit 9b88472
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions core/storage-client/src/storage-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {AlwatrStorageClientConfig} from './type.js';
import type {
AlwatrDocumentObject,
AlwatrDocumentStorage,
AlwatrServiceResponseSuccessWithMeta,
AlwatrServiceResponseSuccess,
StringifyableRecord,
} from '@alwatr/type';

Expand Down Expand Up @@ -117,7 +117,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
this._logger.logMethodArgs?.('get', {storage, documentId});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest<AlwatrServiceResponseSuccessWithMeta<T | null>>({
const responseJson = await serviceRequest<AlwatrServiceResponseSuccess<T | null>>({
...this.fetchOption,
queryParameters: {
storage,
Expand All @@ -144,7 +144,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
this._logger.logMethodArgs?.('has', {storage, documentId});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest({
const responseJson = await serviceRequest<AlwatrServiceResponseSuccess<boolean>>({
...this.fetchOption,
url: this.fetchOption.url + 'has',
queryParameters: {
Expand All @@ -153,14 +153,12 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
},
});

const has = responseJson.data?.has;

if (typeof has !== 'boolean') {
if (typeof responseJson.data !== 'boolean') {
this._logger.error('has', 'invalid_response_data', {responseJson});
throw new Error('invalid_response_data');
}

return has;
return responseJson.data;
}

/**
Expand All @@ -184,7 +182,7 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
this._logger.logMethodArgs?.('set', {storage, documentId: documentObject.id});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest<AlwatrServiceResponseSuccessWithMeta<T>>({
const responseJson = await serviceRequest<AlwatrServiceResponseSuccess<T>>({
...this.fetchOption,
method: 'PATCH',
queryParameters: {
Expand Down Expand Up @@ -284,18 +282,25 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
* await userStorage.delete('user-1');
* ```
*/
async delete(documentId: string, storage: string | undefined = this.config.name): Promise<void> {
async delete(documentId: string, storage: string | undefined = this.config.name): Promise<boolean> {
this._logger.logMethodArgs?.('delete', {storage, documentId});
if (storage == null) throw new Error('storage_not_defined');

await serviceRequest({
const responseJson = await serviceRequest<AlwatrServiceResponseSuccess<boolean>>({
...this.fetchOption,
method: 'DELETE',
queryParameters: {
storage,
id: documentId,
},
});

if (typeof responseJson.data !== 'boolean') {
this._logger.error('delete', 'invalid_response_data', {responseJson});
throw new Error('invalid_response_data');
}

return responseJson.data;
}

/**
Expand All @@ -313,13 +318,13 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
this._logger.logMethodArgs?.('getStorage', {name});
if (name == null) throw new Error('storage_not_defined');

const responseJson = (await serviceRequest({
const responseJson = await serviceRequest<AlwatrDocumentStorage<T>>({
...this.fetchOption,
url: this.fetchOption.url + 'storage',
queryParameters: {
name,
},
})) as AlwatrDocumentStorage<T>;
});

if (
typeof responseJson.data !== 'object' ||
Expand All @@ -346,21 +351,19 @@ export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject = Alw
this._logger.logMethodArgs?.('keys', {storage});
if (storage == null) throw new Error('storage_not_defined');

const responseJson = await serviceRequest({
const responseJson = await serviceRequest<AlwatrServiceResponseSuccess<Array<string>>>({
...this.fetchOption,
url: this.fetchOption.url + 'keys',
queryParameters: {
storage,
},
});

const keys = responseJson.data.keys;

if (!Array.isArray(keys)) {
if (!Array.isArray(responseJson.data)) {
this._logger.error('keys', 'invalid_response_data', {responseJson});
throw new Error('invalid_response_data');
}

return keys as Array<string>;
return responseJson.data;
}
}

0 comments on commit 9b88472

Please sign in to comment.