Skip to content

Commit

Permalink
feat(service/storage): add remove route
Browse files Browse the repository at this point in the history
  • Loading branch information
njfamirm committed Nov 13, 2022
1 parent cae77a3 commit d4452cd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/nano-server/src/nano-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ export class AlwatrConnection {
this.reply({
ok: false,
statusCode: 406,
errorCode: `${param}_QUERY_PARAMETER_REQUIRED`,
errorCode: `${param}_query_parameter_required`,
});
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/nanoservice/storage/demo.http
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ GET {{apiUrl}}/{{apiVersion}}/?storage=comments/page1&id=c1
authorization: Bearer {{token}}

### Insert document
PATCH {{apiUrl}}/{{apiVersion}}/?storage=comments/page1
POST {{apiUrl}}/{{apiVersion}}/?storage=comments/page1
authorization: Bearer {{token}}
Content-Type: application/json

Expand All @@ -34,7 +34,7 @@ Content-Type: application/json
}

### Remove document
DELETE HTTP/1.1 {{apiUrl}}/{{apiVersion}}/?storage=comments/page1&id=c1
DELETE {{apiUrl}}/{{apiVersion}}/?storage=comments/page1&id=c1
authorization: Bearer {{token}}

### Get keys
Expand Down
1 change: 1 addition & 0 deletions packages/nanoservice/storage/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './route/update.js';
import './route/remove.js';
import './route/get.js';
import {logger} from './lib/config.js';

Expand Down
33 changes: 33 additions & 0 deletions packages/nanoservice/storage/src/route/remove.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('DELETE', 'all', removeDocument);

async function removeDocument(connection: AlwatrConnection): Promise<void> {
logger.logMethodArgs('updateDocument', {method: connection.method});

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

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

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

if (storage.remove(param.id) === true) {
connection.reply({
ok: true,
data: {},
});
}
else {
connection.reply({
ok: false,
statusCode: 404,
errorCode: 'document_not_found',
});
}
}

0 comments on commit d4452cd

Please sign in to comment.