Skip to content

Commit

Permalink
feat(storage-client): use document object from fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Nov 27, 2022
1 parent 5346efa commit 1ab7f5d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 32 deletions.
26 changes: 13 additions & 13 deletions packages/core/storage-client/src/storage-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {fetch, FetchOptions} from '@alwatr/fetch';
import {alwatrRegisteredList, createLogger} from '@alwatr/logger';

import type {AlwatrStorageClientConfig, ServerResponse} from './type.js';
import type {DocumentObject} from '@alwatr/storage-engine';
import type {AlwatrStorageClientConfig} from './type.js';
import type {AlwatrDocumentObject, AlwatrServiceResponse} from '@alwatr/fetch';

export {DocumentObject, AlwatrStorageClientConfig};
export {AlwatrStorageClientConfig, AlwatrDocumentObject};

alwatrRegisteredList.push({
name: '@alwatr/storage-client',
Expand All @@ -18,9 +18,9 @@ alwatrRegisteredList.push({
*
* ```ts
* import {AlwatrStorageClient} from '@alwatr/storage-client';
* import type {DocumentObject} from '@alwatr/storage-client';
* import type {AlwatrDocumentObject } from '@alwatr/storage-client';
*
* interface User extends DocumentObject {
* interface User extends AlwatrDocumentObject {
* fname: string;
* lname: string;
* email: string;
Expand Down Expand Up @@ -62,7 +62,7 @@ alwatrRegisteredList.push({
* console.log('delete 404: %o', (err as Error).message);
* }
*/
export class AlwatrStorageClient<DocumentType extends DocumentObject> {
export class AlwatrStorageClient<DocumentType extends AlwatrDocumentObject> {
protected _logger = createLogger('alwatr-storage-client:' + this.config.name, undefined, this.config.debug);

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
},
});

let content: ServerResponse<DocumentType>;
let content: AlwatrServiceResponse<DocumentType>;
try {
content = await response.json();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
},
});

let content: ServerResponse<{has: boolean}>;
let content: AlwatrServiceResponse<{has: boolean}>;
try {
content = await response.json();
}
Expand Down Expand Up @@ -190,7 +190,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
* ```
*/
async set(documentObject: DocumentType): Promise<DocumentType> {
this._logger.logMethodArgs('set', {documentId: documentObject._id});
this._logger.logMethodArgs('set', {documentId: documentObject.id});

const response = await fetch({
...this.fetchOption,
Expand All @@ -201,7 +201,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
bodyJson: documentObject,
});

let content: ServerResponse<DocumentType>;
let content: AlwatrServiceResponse<DocumentType>;
try {
content = await response.json();
}
Expand Down Expand Up @@ -238,7 +238,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
},
});

let content: ServerResponse<Record<string, never>>;
let content: AlwatrServiceResponse<Record<string, never>>;
try {
content = await response.json();
}
Expand Down Expand Up @@ -277,7 +277,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
},
});

let content: ServerResponse<Record<string, DocumentType>>;
let content: AlwatrServiceResponse<Record<string, DocumentType>>;
try {
content = await response.json();
}
Expand Down Expand Up @@ -313,7 +313,7 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
},
});

let content: ServerResponse<{keys: Array<string>}>;
let content: AlwatrServiceResponse<{keys: Array<string>}>;
try {
content = await response.json();
}
Expand Down
19 changes: 0 additions & 19 deletions packages/core/storage-client/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type {DocumentObject} from '@alwatr/storage-engine';

export type AlwatrStorageClientConfig = {
/**
* Storage name (like database name).
Expand Down Expand Up @@ -37,20 +35,3 @@ export type AlwatrStorageClientConfig = {
*/
debug?: boolean;
};

type ServerResponseFailed = {
ok: false;
statusCode: number;
errorCode: string;
data?: Record<string, unknown>;
};

type ServerResponseSuccess<DataType> = {
ok: true;
statusCode?: number;
data: DataType;
};

export type ServerResponse<DataType extends Record<string, unknown> = DocumentObject> =
| ServerResponseSuccess<DataType>
| ServerResponseFailed;

0 comments on commit 1ab7f5d

Please sign in to comment.