Skip to content

Commit

Permalink
feat(service/storage-server): has route
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Nov 17, 2022
1 parent 9a982eb commit 7254393
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/service/storage-server/demo.http
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Content-Type: application/json
"message": "Salam ;)"
}

### Check document exists by storageName and docId
GET {{apiUrl}}/{{apiVersion}}/has?storage=comments/page1&id=c1
authorization: Bearer {{token}}

### Delete document
DELETE {{apiUrl}}/{{apiVersion}}/?storage=comments/page1&id=c1
authorization: Bearer {{token}}
Expand All @@ -71,6 +75,11 @@ GET {{apiUrl}}/{{apiVersion}}/?storage=comments/page1/page1&id=c1
### Page 404 (wrong method)
TRACE {{apiUrl}}/{{apiVersion}}

### Document not exists
GET {{apiUrl}}/{{apiVersion}}/has?storage=comments/page1&id=foo
authorization: Bearer {{token}}


### Get a document by storageName without id
GET {{apiUrl}}/{{apiVersion}}/?storage=comments/page1
authorization: Bearer {{token}}
Expand Down
1 change: 1 addition & 0 deletions packages/service/storage-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './route/get.js';
import './route/has.js';
import './route/patch.js';
import './route/delete.js';
import './route/keys.js';
Expand Down
33 changes: 33 additions & 0 deletions packages/service/storage-server/src/route/has.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {config, logger} from '../lib/config.js';
import {nanoServer} from '../lib/nano-server.js';
import {storageProvider} from '../lib/storage-provider.js';

import type {AlwatrConnection} from '@alwatr/nano-server';

nanoServer.route('GET', '/has', has);

async function has(connection: AlwatrConnection): Promise<void> {
logger.logMethod('has');

const token = connection.requireToken(config.token);
if (token == null) return;

const params = connection.requireQueryParams<{storage: string, id: string}>({storage: 'string', id: 'string'});
if (params == null) return;

const storage = storageProvider.get({name: params.storage});

if (!storage.has(params.id)) {
connection.reply({
ok: false,
errorCode: 'document_not_exists',
statusCode: 404,
});
return;
}

connection.reply({
ok: true,
data: {},
});
}

0 comments on commit 7254393

Please sign in to comment.