Skip to content

Commit

Permalink
Achalhii/dev 6165/replace @awaitjs by express-async-errors (#284)
Browse files Browse the repository at this point in the history
* improvement: replace lib

* improvement: replace router for services

* feat: replace async routes
  • Loading branch information
Achalhii committed Apr 29, 2024
1 parent 275de6f commit 1ad4ee4
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 59 deletions.
3 changes: 2 additions & 1 deletion lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const CampsiService = require('./service');
const debug = require('debug')('campsi');
const MQTTEmitter = require('mqtt-emitter');
const express = require('express');
const async = require('async');
require('async');
require('express-async-errors');
const { MongoClient } = require('mongodb');
const { URL } = require('url');
const { ValidationError } = require('express-json-validator-middleware');
Expand Down
2 changes: 1 addition & 1 deletion lib/service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Router } = require('@awaitjs/express');
const { Router } = require('express');

/**
* Abstract class of each service mounted in campsi
Expand Down
48 changes: 22 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"mongodb": "^6.3.0"
},
"dependencies": {
"@awaitjs/express": "^0.9.0",
"@aws-sdk/client-s3": "^3.398.0",
"@aws-sdk/lib-storage": "^3.400.0",
"@passport-next/passport": "^2.1.1",
Expand All @@ -67,6 +66,7 @@
"edit-url": "^1.0.1",
"eslint-config-axeptio": "^1.0.0",
"eslint-plugin-n": "^15.2.0",
"express-async-errors": "^3.1.1",
"express-json-validator-middleware": "^2.2.1",
"express-session": "^1.16.1",
"http-errors": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions services/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ module.exports = class AuthService extends CampsiService {
router.post('/invitations', handlers.inviteUser);
router.get('/invitations/:invitationToken', limiter, handlers.getUserByInvitationToken);
router.post('/invitations/:invitationToken', handlers.acceptInvitation);
router.deleteAsync('/invitations/:invitationToken', handlers.deleteInvitation);
router.delete('/invitations/:invitationToken', handlers.deleteInvitation);
router.put('/tokens', handlers.tokenMaintenance);

if (providers.local) {
router.use('/local', local.middleware(providers.local));
router.post('/local/signup', local.signup);
router.post('/local/signin', local.signin);
router.postAsync('/local/reset-password-token', validatePasswordResetUrl, local.createResetPasswordToken);
router.post('/local/reset-password-token', validatePasswordResetUrl, local.createResetPasswordToken);
router.post('/local/reset-password', local.resetPassword);
router.get('/local/validate', local.validate);
router.put('/local/update-password', local.updatePassword);
Expand Down
6 changes: 3 additions & 3 deletions services/docs/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ module.exports = class DocsService extends CampsiService {
this.router.post('/:resource/:id/:state/locks', additionalMiddlewares, handlers.lockDocument);
this.router.get('/:resource/:id/:state', additionalMiddlewares, handlers.getDoc);
this.router.get('/:resource/:id', additionalMiddlewares, handlers.getDoc);
this.router.postAsync('/:resource/:state', additionalMiddlewares, validateWriteAccess, handlers.postDoc);
this.router.post('/:resource/:state', additionalMiddlewares, validateWriteAccess, handlers.postDoc);
this.router.post('/:resource', additionalMiddlewares, validateWriteAccess, handlers.postDoc);
this.router.put('/:resource/:id/state', additionalMiddlewares, validateWriteAccess, handlers.putDocState);
this.router.putAsync('/:resource/:id/:state', additionalMiddlewares, validateWriteAccess, handlers.putDoc);
this.router.putAsync('/:resource/:id', additionalMiddlewares, validateWriteAccess, handlers.putDoc);
this.router.put('/:resource/:id/:state', additionalMiddlewares, validateWriteAccess, handlers.putDoc);
this.router.put('/:resource/:id', additionalMiddlewares, validateWriteAccess, handlers.putDoc);
this.router.patch('/:resource/:id', additionalMiddlewares, validateWriteAccess, handlers.patchDoc);
this.router.delete('/:resource/:id', additionalMiddlewares, handlers.delDoc);
this.router.delete('/:resource/:id/:state', additionalMiddlewares, handlers.delDoc);
Expand Down
12 changes: 6 additions & 6 deletions services/notifications/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ module.exports = class NotificationsService extends CampsiService {

this.router.param('resource', param.attachResource(service.options));
this.router.get('/', handlers.getResources);
this.router.getAsync('/:resource/', handlers.getNotifications);
this.router.getAsync('/:resource/:id', handlers.getNotification);
this.router.postAsync('/:resource/', handlers.createNotification);
this.router.putAsync('/:resource/:id', handlers.updateNotification);
this.router.patchAsync('/:resource/:id', handlers.patchNotification);
this.router.deleteAsync('/:resource/:id', handlers.deleteNotification);
this.router.get('/:resource/', handlers.getNotifications);
this.router.get('/:resource/:id', handlers.getNotification);
this.router.post('/:resource/', handlers.createNotification);
this.router.put('/:resource/:id', handlers.updateNotification);
this.router.patch('/:resource/:id', handlers.patchNotification);
this.router.delete('/:resource/:id', handlers.deleteNotification);

return new Promise(resolve => {
const ajvWriter = new Ajv({ allErrors: true, useAssign: true, strictTuples: false, strict: false });
Expand Down
12 changes: 6 additions & 6 deletions services/stripe-billing/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ module.exports = class StripeBillingService extends CampsiService {
);
});

this.router.getAsync('/subscriptions/:id[:]get-next-invoice', async (req, res) => {
this.router.get('/subscriptions/:id[:]get-next-invoice', async (req, res) => {
const subscriptionId = req.params.id;
const subscription = await stripe.subscriptions.retrieve(subscriptionId);
const nextInvoice = await stripe.invoices.retrieveUpcoming({
Expand Down Expand Up @@ -221,11 +221,11 @@ module.exports = class StripeBillingService extends CampsiService {
stripe.invoices.retrieve(req.params.id, optionsFromQuery(req.query), defaultHandler(res));
});

this.router.getAsync('/subscription-schedules/:id', (req, res) => {
this.router.get('/subscription-schedules/:id', (req, res) => {
stripe.subscriptionSchedules.retrieve(req.params.id, optionsFromQuery(req.query), defaultHandler(res));
});

this.router.postAsync('/subscription-schedules', (req, res) => {
this.router.post('/subscription-schedules', (req, res) => {
const params = {
customer: req.body.customer,
metadata: req.body.metadata,
Expand All @@ -240,7 +240,7 @@ module.exports = class StripeBillingService extends CampsiService {
stripe.subscriptionSchedules.create(params, { idempotencyKey }, defaultHandler(res));
});

this.router.putAsync('/subscription-schedules/:id', (req, res) => {
this.router.put('/subscription-schedules/:id', (req, res) => {
stripe.subscriptionSchedules.update(
req.params.id,
{
Expand All @@ -255,7 +255,7 @@ module.exports = class StripeBillingService extends CampsiService {
);
});

this.router.postAsync('/subscription-schedules[:]list-all', async (req, res) => {
this.router.post('/subscription-schedules[:]list-all', async (req, res) => {
const schedules = [];
const params = {
customer: req.body.customer,
Expand Down Expand Up @@ -293,7 +293,7 @@ module.exports = class StripeBillingService extends CampsiService {
res.json(schedules);
});

this.router.deleteAsync('/subscription-schedules/:id[:]release', (req, res) => {
this.router.delete('/subscription-schedules/:id[:]release', (req, res) => {
stripe.subscriptionSchedules.release(
req.params.id,
{ preserve_cancel_date: req.body.preserve_cancel_date },
Expand Down
26 changes: 13 additions & 13 deletions services/versioned-docs/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ module.exports = class VersionedDocsService extends CampsiService {
});
this.router.param('resource', param.attachResource(service.options));
this.router.get('/', handlers.getResources);
this.router.getAsync('/:resource', handlers.getDocuments);
this.router.getAsync('/:resource/:id/users', handlers.getDocUsers);
this.router.postAsync('/:resource/:id/users', handlers.postDocUser);
this.router.deleteAsync('/:resource/:id/users/:user', handlers.delDocUser);
this.router.getAsync('/:resource/:id/revisions/', handlers.getDocRevisions);
this.router.getAsync('/:resource/:id/revisions/:revision', handlers.getDocRevision);
this.router.postAsync('/:resource/:id/revisions/:revision[:]set-as-version', handlers.setDocVersion);
this.router.getAsync('/:resource/:id/versions/', handlers.getDocVersions);
this.router.getAsync('/:resource/:id/versions/:version', handlers.getDocVersion);
this.router.getAsync('/:resource/:id', handlers.getDoc);
this.router.postAsync('/:resource', validateWriteAccess, handlers.postDoc);
this.router.patchAsync('/:resource/:id', validateWriteAccess, handlers.updateDoc);
this.router.deleteAsync('/:resource/:id', handlers.delDoc);
this.router.get('/:resource', handlers.getDocuments);
this.router.get('/:resource/:id/users', handlers.getDocUsers);
this.router.post('/:resource/:id/users', handlers.postDocUser);
this.router.delete('/:resource/:id/users/:user', handlers.delDocUser);
this.router.get('/:resource/:id/revisions/', handlers.getDocRevisions);
this.router.get('/:resource/:id/revisions/:revision', handlers.getDocRevision);
this.router.post('/:resource/:id/revisions/:revision[:]set-as-version', handlers.setDocVersion);
this.router.get('/:resource/:id/versions/', handlers.getDocVersions);
this.router.get('/:resource/:id/versions/:version', handlers.getDocVersion);
this.router.get('/:resource/:id', handlers.getDoc);
this.router.post('/:resource', validateWriteAccess, handlers.postDoc);
this.router.patch('/:resource/:id', validateWriteAccess, handlers.updateDoc);
this.router.delete('/:resource/:id', handlers.delDoc);

const ajvWriter = new Ajv({ allErrors: true, useAssign: true, strict: false });
ajvErrors(ajvWriter);
Expand Down

0 comments on commit 1ad4ee4

Please sign in to comment.