@gls_spain/gps-common 27.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +11 -0
- package/.idea/codeStyles/Project.xml +37 -0
- package/.idea/codeStyles/codeStyleConfig.xml +5 -0
- package/.idea/gps-common.iml +12 -0
- package/.idea/inspectionProfiles/Project_Default.xml +7 -0
- package/.idea/jsLibraryMappings.xml +6 -0
- package/.idea/misc.xml +6 -0
- package/.idea/modules.xml +8 -0
- package/.idea/vcs.xml +6 -0
- package/.idea/watcherTasks.xml +45 -0
- package/.prettierignore +5 -0
- package/.prettierrc.json +13 -0
- package/.releaserc +17 -0
- package/CHANGELOG.md +848 -0
- package/README.md +122 -0
- package/errors/DatabaseNotEmptyError.js +9 -0
- package/errors/DefaultsDoNotExistError.js +9 -0
- package/errors/ExpeditionAlreadyExistsError.js +9 -0
- package/errors/ExpeditionCannotBeDeletedError.js +9 -0
- package/errors/ExpeditionCannotBeUpdatedError.js +9 -0
- package/errors/ExpeditionDoesNotExistError.js +9 -0
- package/errors/JobDoesNotExistError.js +9 -0
- package/errors/NetworkAlreadyExistsError.js +9 -0
- package/errors/NetworkCannotBeDeletedError.js +9 -0
- package/errors/NetworkDoesNotExistError.js +9 -0
- package/errors/RepresentativeAlreadyExistsError.js +9 -0
- package/errors/RepresentativeCannotBeDeletedError.js +9 -0
- package/errors/RepresentativeDoesNotExistError.js +9 -0
- package/errors/ShopAlreadyExistsError.js +9 -0
- package/errors/ShopCannotBeDeletedError.js +9 -0
- package/errors/ShopDoesNotExistError.js +9 -0
- package/errors/ShopNotEnabledError.js +9 -0
- package/errors/UserAlreadyExistsError.js +9 -0
- package/errors/UserCannotBeDeletedError.js +9 -0
- package/errors/UserCannotBeUpdatedError.js +9 -0
- package/errors/UserDoesNotExistError.js +9 -0
- package/errors/WrongNetworkError.js +9 -0
- package/errors/WrongPasswordError.js +9 -0
- package/errors/WrongShopError.js +9 -0
- package/errors/ZoneAlreadyExistsError.js +9 -0
- package/errors/ZoneCannotBeDeletedError.js +9 -0
- package/errors/ZoneDoesNotExistError.js +9 -0
- package/errors/index.js +88 -0
- package/index.js +9 -0
- package/index.test.js +35 -0
- package/jest.config.js +5 -0
- package/open-api-spec/index.js +2920 -0
- package/package.json +68 -0
- package/schemas/definitions/become.js +5 -0
- package/schemas/definitions/common.js +172 -0
- package/schemas/definitions/defaults.js +84 -0
- package/schemas/definitions/expeditions.js +637 -0
- package/schemas/definitions/init.js +37 -0
- package/schemas/definitions/integration-events.js +127 -0
- package/schemas/definitions/jobs.js +124 -0
- package/schemas/definitions/login.js +38 -0
- package/schemas/definitions/logout.js +8 -0
- package/schemas/definitions/networks.js +107 -0
- package/schemas/definitions/pods.js +37 -0
- package/schemas/definitions/representatives.js +178 -0
- package/schemas/definitions/shops.js +352 -0
- package/schemas/definitions/users.js +373 -0
- package/schemas/definitions/zones.js +92 -0
- package/schemas/index.js +335 -0
- package/scripts/build-documentation.js +40 -0
- package/scripts/build-json-schemas.js +35 -0
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# @toomanymelons/gls-parcel-shops-common
|
|
2
|
+
|
|
3
|
+
Errors, schemas and documentation to use on both the backend and the frontend
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @toomanymelons/gls-parcel-shops-common
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This is a **@toomanymelons** private package so you need an [npm authentication token](https://docs.npmjs.com/about-authentication-tokens) to be able to install it.
|
|
12
|
+
|
|
13
|
+
Also remember to include a [.npmrc file](https://docs.npmjs.com/using-private-packages-in-a-ci-cd-workflow) at the root of your project so that when you deploy the project to production, the deployment server (or Google App Engine, Heroku, etc) is able to install it.
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
This package provides error classes, json schemas for route validation and an OpenAPI specification.
|
|
18
|
+
|
|
19
|
+
It is recommended that you import what you need individually (see below), but you can also import the entire package at once:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
const { errors, openApiSpec, schemas } = require('@toomanymelons/gls-parcel-shops-common');
|
|
23
|
+
|
|
24
|
+
const { DatabaseNotEmptyError } = errors;
|
|
25
|
+
|
|
26
|
+
const { loginResponse } = schemas;
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### Errors
|
|
30
|
+
|
|
31
|
+
You can import all the errors at once:
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
const errors = require('@toomanymelons/gls-parcel-shops-common/errors');
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Or you can import them individually:
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
const DatabaseNotEmptyError = require('@toomanymelons/gls-parcel-shops-common/errors/DatabaseNotEmptyError');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
There is also a list of all the error names:
|
|
44
|
+
|
|
45
|
+
```javascript
|
|
46
|
+
const { errorsNames } = require('@toomanymelons/gls-parcel-shops-common/errors');
|
|
47
|
+
|
|
48
|
+
console.log(errorsNames.UserDoesNotExistError);
|
|
49
|
+
// UserDoesNotExistError
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### JSON Schemas
|
|
53
|
+
|
|
54
|
+
The provided json schemas are the requests and responses schemas of the API. They can be used for validation and unit testing.
|
|
55
|
+
|
|
56
|
+
You can import all of them at once:
|
|
57
|
+
|
|
58
|
+
```javascript
|
|
59
|
+
const schemas = require('@toomanymelons/gls-parcel-shops-common/schemas');
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Or each one of them individually:
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
const loginResponse = require('@toomanymelons/gls-parcel-shops-common/schemas/login-response');
|
|
66
|
+
const Ajv = require('ajv');
|
|
67
|
+
const validator = Ajv();
|
|
68
|
+
validator.addSchema(loginResponse);
|
|
69
|
+
|
|
70
|
+
const myResponse = {
|
|
71
|
+
user: {
|
|
72
|
+
_id: '2d34620b-c67d-42ec-853d-2541a964df56',
|
|
73
|
+
createdAt: '2019-05-23 09:00:00',
|
|
74
|
+
updatedAt: '2019-05-23 09:00:00',
|
|
75
|
+
username: 'bob',
|
|
76
|
+
deletable: false,
|
|
77
|
+
loginEnabled: true,
|
|
78
|
+
role: 'superAdmin',
|
|
79
|
+
name: 'Super administrator',
|
|
80
|
+
emails: [],
|
|
81
|
+
phones: [],
|
|
82
|
+
tags: [],
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const valid = validator.validate(loginResponse, myResponse); // true
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### OpenAPI specification
|
|
90
|
+
|
|
91
|
+
The exported `openApiSpec` is an OpenAPI specification that can be used to build a documentation. If you need a static HTML version see scripts below.
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
const openApiSpec = require('@toomanymelons/gls-parcel-shops-common/open-api-spec');
|
|
95
|
+
|
|
96
|
+
console.log(openApiSpec);
|
|
97
|
+
/*
|
|
98
|
+
{
|
|
99
|
+
openapi: '3.0.2'
|
|
100
|
+
info: {
|
|
101
|
+
title: 'GLS Parcel Shops API',
|
|
102
|
+
description: 'This is the API backend for the GLS Parcel Shops project.',
|
|
103
|
+
contact: { name: 'Cristóbal Valverde Albo', email: 'cristobal@albo.dev' },
|
|
104
|
+
license: { name: 'NO LICENSE', url: 'https://choosealicense.com/no-permission/' },
|
|
105
|
+
version: 'v1.0.0',
|
|
106
|
+
},
|
|
107
|
+
...
|
|
108
|
+
}
|
|
109
|
+
*/
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Scripts
|
|
113
|
+
|
|
114
|
+
This package provides two scripts, one to build a static version of the json schemas and one to build a [Redoc](https://rebilly.github.io/ReDoc/) HTML with the OpenAPI documentation.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
build-gls-parcel-shops-json-schemas --output=build/gls-schemas
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
```bash
|
|
121
|
+
build-gls-parcel-shops-documentation --output=build/gls-documentation.html
|
|
122
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class DatabaseNotEmptyError extends Error {
|
|
2
|
+
constructor(message = 'The database cannot be initialized because it is not empty.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'DatabaseNotEmptyError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = DatabaseNotEmptyError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class ExpeditionAlreadyExistsError extends Error {
|
|
2
|
+
constructor(message = 'Expedition already exists.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'ExpeditionAlreadyExistsError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = ExpeditionAlreadyExistsError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class ExpeditionCannotBeDeletedError extends Error {
|
|
2
|
+
constructor(message = 'Expedition cannot be deleted.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'ExpeditionCannotBeDeletedError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = ExpeditionCannotBeDeletedError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class ExpeditionCannotBeUpdatedError extends Error {
|
|
2
|
+
constructor(message = 'Expedition cannot be updated.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'ExpeditionCannotBeUpdatedError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = ExpeditionCannotBeUpdatedError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class ExpeditionDoesNotExistError extends Error {
|
|
2
|
+
constructor(message = 'Expedition does not exist.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'ExpeditionDoesNotExistError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = ExpeditionDoesNotExistError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class NetworkCannotBeDeletedError extends Error {
|
|
2
|
+
constructor(message = 'Network cannot be deleted.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'NetworkCannotBeDeletedError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = NetworkCannotBeDeletedError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class RepresentativeAlreadyExistsError extends Error {
|
|
2
|
+
constructor(message = 'Representative already exists.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'RepresentativeAlreadyExistsError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = RepresentativeAlreadyExistsError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class RepresentativeCannotBeDeletedError extends Error {
|
|
2
|
+
constructor(message = 'Representative cannot be deleted.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'RepresentativeCannotBeDeletedError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = RepresentativeCannotBeDeletedError;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
class RepresentativeDoesNotExistError extends Error {
|
|
2
|
+
constructor(message = 'Representative does not exist.') {
|
|
3
|
+
// noinspection JSCheckFunctionSignatures
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = 'RepresentativeDoesNotExistError';
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
module.exports = RepresentativeDoesNotExistError;
|
package/errors/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const DatabaseNotEmptyError = require('./DatabaseNotEmptyError');
|
|
2
|
+
const UserDoesNotExistError = require('./UserDoesNotExistError');
|
|
3
|
+
const UserAlreadyExistsError = require('./UserAlreadyExistsError');
|
|
4
|
+
const UserCannotBeUpdatedError = require('./UserCannotBeUpdatedError');
|
|
5
|
+
const UserCannotBeDeletedError = require('./UserCannotBeDeletedError');
|
|
6
|
+
const NetworkDoesNotExistError = require('./NetworkDoesNotExistError');
|
|
7
|
+
const NetworkAlreadyExistsError = require('./NetworkAlreadyExistsError');
|
|
8
|
+
const NetworkCannotBeDeletedError = require('./NetworkCannotBeDeletedError');
|
|
9
|
+
const WrongNetworkError = require('./WrongNetworkError');
|
|
10
|
+
const ShopDoesNotExistError = require('./ShopDoesNotExistError');
|
|
11
|
+
const ShopAlreadyExistsError = require('./ShopAlreadyExistsError');
|
|
12
|
+
const ShopCannotBeDeletedError = require('./ShopCannotBeDeletedError');
|
|
13
|
+
const ShopNotEnabledError = require('./ShopNotEnabledError');
|
|
14
|
+
const WrongShopError = require('./WrongShopError');
|
|
15
|
+
const WrongPasswordError = require('./WrongPasswordError');
|
|
16
|
+
const ExpeditionDoesNotExistError = require('./ExpeditionDoesNotExistError');
|
|
17
|
+
const ExpeditionAlreadyExistsError = require('./ExpeditionAlreadyExistsError');
|
|
18
|
+
const ExpeditionCannotBeUpdatedError = require('./ExpeditionCannotBeUpdatedError');
|
|
19
|
+
const ExpeditionCannotBeDeletedError = require('./ExpeditionCannotBeDeletedError');
|
|
20
|
+
const DefaultsDoNotExistError = require('./DefaultsDoNotExistError');
|
|
21
|
+
const JobDoesNotExistError = require('./JobDoesNotExistError');
|
|
22
|
+
const RepresentativeDoesNotExistError = require('./RepresentativeDoesNotExistError');
|
|
23
|
+
const RepresentativeAlreadyExistsError = require('./RepresentativeAlreadyExistsError');
|
|
24
|
+
const RepresentativeCannotBeDeletedError = require('./RepresentativeCannotBeDeletedError');
|
|
25
|
+
const ZoneDoesNotExistError = require('./ZoneDoesNotExistError');
|
|
26
|
+
const ZoneAlreadyExistsError = require('./ZoneAlreadyExistsError');
|
|
27
|
+
const ZoneCannotBeDeletedError = require('./ZoneCannotBeDeletedError');
|
|
28
|
+
|
|
29
|
+
const errorsNames = {
|
|
30
|
+
DatabaseNotEmptyError: 'DatabaseNotEmptyError',
|
|
31
|
+
UserDoesNotExistError: 'UserDoesNotExistError',
|
|
32
|
+
UserAlreadyExistsError: 'UserAlreadyExistsError',
|
|
33
|
+
UserCannotBeUpdatedError: 'UserCannotBeUpdatedError',
|
|
34
|
+
UserCannotBeDeletedError: 'UserCannotBeDeletedError',
|
|
35
|
+
NetworkDoesNotExistError: 'NetworkDoesNotExistError',
|
|
36
|
+
NetworkAlreadyExistsError: 'NetworkAlreadyExistsError',
|
|
37
|
+
NetworkCannotBeDeletedError: 'NetworkCannotBeDeletedError',
|
|
38
|
+
WrongNetworkError: 'WrongNetworkError',
|
|
39
|
+
ShopDoesNotExistError: 'ShopDoesNotExistError',
|
|
40
|
+
ShopAlreadyExistsError: 'ShopAlreadyExistsError',
|
|
41
|
+
ShopCannotBeDeletedError: 'ShopCannotBeDeletedError',
|
|
42
|
+
ShopNotEnabledError: 'ShopNotEnabledError',
|
|
43
|
+
WrongShopError: 'WrongShopError',
|
|
44
|
+
WrongPasswordError: 'WrongPasswordError',
|
|
45
|
+
ExpeditionDoesNotExistError: 'ExpeditionDoesNotExistError',
|
|
46
|
+
ExpeditionAlreadyExistsError: 'ExpeditionAlreadyExistsError',
|
|
47
|
+
ExpeditionCannotBeUpdatedError: 'ExpeditionCannotBeUpdatedError',
|
|
48
|
+
ExpeditionCannotBeDeletedError: 'ExpeditionCannotBeDeletedError',
|
|
49
|
+
DefaultsDoNotExistError: 'DefaultsDoNotExistError',
|
|
50
|
+
JobDoesNotExistError: 'JobDoesNotExistError',
|
|
51
|
+
RepresentativeDoesNotExistError: 'RepresentativeDoesNotExistError',
|
|
52
|
+
RepresentativeAlreadyExistsError: 'RepresentativeAlreadyExistsError',
|
|
53
|
+
RepresentativeCannotBeDeletedError: 'RepresentativeCannotBeDeletedError',
|
|
54
|
+
ZoneDoesNotExistError: 'ZoneDoesNotExistError',
|
|
55
|
+
ZoneAlreadyExistsError: 'ZoneAlreadyExistsError',
|
|
56
|
+
ZoneCannotBeDeletedError: 'ZoneCannotBeDeletedError',
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
module.exports = {
|
|
60
|
+
DatabaseNotEmptyError,
|
|
61
|
+
UserDoesNotExistError,
|
|
62
|
+
UserAlreadyExistsError,
|
|
63
|
+
UserCannotBeUpdatedError,
|
|
64
|
+
UserCannotBeDeletedError,
|
|
65
|
+
NetworkDoesNotExistError,
|
|
66
|
+
NetworkAlreadyExistsError,
|
|
67
|
+
NetworkCannotBeDeletedError,
|
|
68
|
+
WrongNetworkError,
|
|
69
|
+
ShopDoesNotExistError,
|
|
70
|
+
ShopAlreadyExistsError,
|
|
71
|
+
ShopCannotBeDeletedError,
|
|
72
|
+
ShopNotEnabledError,
|
|
73
|
+
WrongShopError,
|
|
74
|
+
WrongPasswordError,
|
|
75
|
+
ExpeditionDoesNotExistError,
|
|
76
|
+
ExpeditionAlreadyExistsError,
|
|
77
|
+
ExpeditionCannotBeUpdatedError,
|
|
78
|
+
ExpeditionCannotBeDeletedError,
|
|
79
|
+
DefaultsDoNotExistError,
|
|
80
|
+
JobDoesNotExistError,
|
|
81
|
+
RepresentativeDoesNotExistError,
|
|
82
|
+
RepresentativeAlreadyExistsError,
|
|
83
|
+
RepresentativeCannotBeDeletedError,
|
|
84
|
+
ZoneDoesNotExistError,
|
|
85
|
+
ZoneAlreadyExistsError,
|
|
86
|
+
ZoneCannotBeDeletedError,
|
|
87
|
+
errorsNames,
|
|
88
|
+
};
|
package/index.js
ADDED
package/index.test.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const Ajv = require('ajv');
|
|
2
|
+
const { lenientDatetimeFormat, timezoneFormat, normalizeKeyword } = require('@gls_spain/ajv-options');
|
|
3
|
+
const { errors, openApiSpec, schemas } = require('./index');
|
|
4
|
+
|
|
5
|
+
describe('#errors', () => {
|
|
6
|
+
test('should all be subclasses of Error', () => {
|
|
7
|
+
Object.values(errors)
|
|
8
|
+
.filter(error => typeof error === 'function')
|
|
9
|
+
.forEach(ErrorClass => {
|
|
10
|
+
expect(ErrorClass.prototype).toBeInstanceOf(Error);
|
|
11
|
+
const error = new ErrorClass();
|
|
12
|
+
expect(error.name).toBe(ErrorClass.name);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('#openApiSpec', () => {
|
|
18
|
+
test('should be an object', () => {
|
|
19
|
+
expect(openApiSpec).toBeInstanceOf(Object);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('#schemas', () => {
|
|
24
|
+
test('should all be unique valid json schemas', () => {
|
|
25
|
+
const ajv = Ajv({ formats: { 'date-time': lenientDatetimeFormat, timezone: timezoneFormat } });
|
|
26
|
+
ajv.addKeyword('normalize', normalizeKeyword);
|
|
27
|
+
Object.values(schemas).forEach(schema => {
|
|
28
|
+
try {
|
|
29
|
+
ajv.compile(schema);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error(`schema ${schema.$id} error: ${error.message}`);
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|