Skip to content

Commit

Permalink
feat(storage-client): has method
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Nov 17, 2022
1 parent 7254393 commit ba64195
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions packages/core/storage-client/src/storage-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,50 @@ export class AlwatrStorageClient<DocumentType extends DocumentObject> {
}
}

/**
* Check document exists by id.
*
* @param documentId The id of the document object.
*
* Example:
*
* ```ts
* const isUserExists = await userStorage.has('user-1');
* if (!isUserExists) console.log('user_not_found')
* ```
*/
async has(documentId: string): Promise<boolean> {
const response = await fetch({
url: this.config.host,
queryParameters: {
storage: this.config.name,
id: documentId,
},
headers: {
'Authorization': `Bearer ${this.config.token}`,
},
timeout: this.config.timeout,
});

let content: ServerResponse<DocumentType>;
try {
content = await response.json();
}
catch {
throw new Error('invalid_json');
}

if (content.ok === true) {
return true;
}
else if (content.ok === false && content.errorCode === 'document_not_found') {
return false;
}
else {
throw new Error('fetch_failed');
}
}

/**
* Insert/update a document object in the storage.
*
Expand Down

0 comments on commit ba64195

Please sign in to comment.