@opra/core 0.27.2 → 0.28.1

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.
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ const fs_1 = tslib_1.__importDefault(require("fs"));
5
+ const path_1 = tslib_1.__importDefault(require("path"));
6
+ const common_1 = require("@opra/common");
7
+ common_1.I18n.prototype.loadResourceBundle = async function (lang, ns, filePath, deep, overwrite) {
8
+ let obj;
9
+ if ((0, common_1.isUrlString)(filePath)) {
10
+ obj = (await fetch(filePath, { headers: { accept: 'application/json' } })).json();
11
+ }
12
+ else {
13
+ const content = fs_1.default.readFileSync(filePath, 'utf8');
14
+ obj = JSON.parse(content);
15
+ }
16
+ this.addResourceBundle(lang, ns, obj, deep, overwrite);
17
+ };
18
+ common_1.I18n.prototype.loadResourceDir = async function (dirnames, deep, overwrite) {
19
+ for (const dirname of Array.isArray(dirnames) ? dirnames : [dirnames]) {
20
+ /* istanbul ignore next */
21
+ if (!(fs_1.default.existsSync(dirname)))
22
+ continue;
23
+ const languageDirs = fs_1.default.readdirSync(dirname);
24
+ for (const lang of languageDirs) {
25
+ const langDir = path_1.default.join(dirname, lang);
26
+ if ((fs_1.default.statSync(langDir)).isDirectory()) {
27
+ const nsDirs = fs_1.default.readdirSync(langDir);
28
+ for (const nsfile of nsDirs) {
29
+ const nsFilePath = path_1.default.join(langDir, nsfile);
30
+ const ext = path_1.default.extname(nsfile);
31
+ if (ext === '.json' && (fs_1.default.statSync(nsFilePath)).isFile()) {
32
+ const ns = path_1.default.basename(nsfile, ext);
33
+ await this.loadResourceBundle(lang, ns, nsFilePath, deep, overwrite);
34
+ }
35
+ }
36
+ }
37
+ }
38
+ }
39
+ };
@@ -90,7 +90,7 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
90
90
  throw e;
91
91
  if (e instanceof vg.ValidationError) {
92
92
  throw new common_1.BadRequestError({
93
- message: (0, common_1.translate)('error:RESPONSE_VALIDATION,', 'Response validation failed'),
93
+ message: (0, common_1.translate)('error:RESPONSE_VALIDATION,'),
94
94
  code: 'RESPONSE_VALIDATION',
95
95
  details: e.issues
96
96
  }, e);
@@ -109,7 +109,7 @@ class HttpAdapterHost extends platform_adapter_host_js_1.PlatformAdapterHost {
109
109
  throw e;
110
110
  if (e instanceof vg.ValidationError) {
111
111
  throw new common_1.InternalServerError({
112
- message: (0, common_1.translate)('error:RESPONSE_VALIDATION,', 'Response validation failed'),
112
+ message: (0, common_1.translate)('error:RESPONSE_VALIDATION,'),
113
113
  code: 'RESPONSE_VALIDATION',
114
114
  details: e.issues
115
115
  }, e);
package/cjs/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  require("reflect-metadata");
5
+ require("./augmentation/18n.augmentation.js");
5
6
  require("./augmentation/resource.augmentation.js");
6
7
  require("./augmentation/collection.augmentation.js");
7
8
  require("./augmentation/container.augmentation.js");
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PlatformAdapterHost = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ require("./augmentation/18n.augmentation.js");
5
6
  const path_1 = tslib_1.__importDefault(require("path"));
6
7
  const putil_varhelpers_1 = require("putil-varhelpers");
7
8
  const strict_typed_events_1 = require("strict-typed-events");
@@ -114,7 +115,7 @@ class PlatformAdapterHost extends strict_typed_events_1.AsyncEventEmitter {
114
115
  return { controller, endpoint, handler };
115
116
  }
116
117
  throw new common_1.BadRequestError({
117
- message: (0, common_1.translate)('ACTION_NOT_FOUND', { resource: resource.name, action: name }, `The {{resource}} resource doesn't have an action named '{{action}}'`),
118
+ message: (0, common_1.translate)('error:ACTION_NOT_FOUND', { resource: resource.name, action: name }),
118
119
  severity: 'error',
119
120
  code: 'ACTION_NOT_FOUND'
120
121
  });
@@ -131,7 +132,7 @@ class PlatformAdapterHost extends strict_typed_events_1.AsyncEventEmitter {
131
132
  return { controller, endpoint, handler };
132
133
  }
133
134
  throw new common_1.ForbiddenError({
134
- message: (0, common_1.translate)('OPERATION_FORBIDDEN', { resource: resource.name, operation: name }, `'The {{resource}} resource does not accept '{{operation}}' operations`),
135
+ message: (0, common_1.translate)('error:OPERATION_FORBIDDEN', { resource: resource.name, operation: name }),
135
136
  severity: 'error',
136
137
  code: 'OPERATION_FORBIDDEN'
137
138
  });
@@ -0,0 +1,36 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { I18n, isUrlString } from '@opra/common';
4
+ I18n.prototype.loadResourceBundle = async function (lang, ns, filePath, deep, overwrite) {
5
+ let obj;
6
+ if (isUrlString(filePath)) {
7
+ obj = (await fetch(filePath, { headers: { accept: 'application/json' } })).json();
8
+ }
9
+ else {
10
+ const content = fs.readFileSync(filePath, 'utf8');
11
+ obj = JSON.parse(content);
12
+ }
13
+ this.addResourceBundle(lang, ns, obj, deep, overwrite);
14
+ };
15
+ I18n.prototype.loadResourceDir = async function (dirnames, deep, overwrite) {
16
+ for (const dirname of Array.isArray(dirnames) ? dirnames : [dirnames]) {
17
+ /* istanbul ignore next */
18
+ if (!(fs.existsSync(dirname)))
19
+ continue;
20
+ const languageDirs = fs.readdirSync(dirname);
21
+ for (const lang of languageDirs) {
22
+ const langDir = path.join(dirname, lang);
23
+ if ((fs.statSync(langDir)).isDirectory()) {
24
+ const nsDirs = fs.readdirSync(langDir);
25
+ for (const nsfile of nsDirs) {
26
+ const nsFilePath = path.join(langDir, nsfile);
27
+ const ext = path.extname(nsfile);
28
+ if (ext === '.json' && (fs.statSync(nsFilePath)).isFile()) {
29
+ const ns = path.basename(nsfile, ext);
30
+ await this.loadResourceBundle(lang, ns, nsFilePath, deep, overwrite);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ };
@@ -86,7 +86,7 @@ export class HttpAdapterHost extends PlatformAdapterHost {
86
86
  throw e;
87
87
  if (e instanceof vg.ValidationError) {
88
88
  throw new BadRequestError({
89
- message: translate('error:RESPONSE_VALIDATION,', 'Response validation failed'),
89
+ message: translate('error:RESPONSE_VALIDATION,'),
90
90
  code: 'RESPONSE_VALIDATION',
91
91
  details: e.issues
92
92
  }, e);
@@ -105,7 +105,7 @@ export class HttpAdapterHost extends PlatformAdapterHost {
105
105
  throw e;
106
106
  if (e instanceof vg.ValidationError) {
107
107
  throw new InternalServerError({
108
- message: translate('error:RESPONSE_VALIDATION,', 'Response validation failed'),
108
+ message: translate('error:RESPONSE_VALIDATION,'),
109
109
  code: 'RESPONSE_VALIDATION',
110
110
  details: e.issues
111
111
  }, e);
package/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import "reflect-metadata";
2
+ import './augmentation/18n.augmentation.js';
2
3
  import './augmentation/resource.augmentation.js';
3
4
  import './augmentation/collection.augmentation.js';
4
5
  import './augmentation/container.augmentation.js';
@@ -1,3 +1,4 @@
1
+ import './augmentation/18n.augmentation.js';
1
2
  import path from 'path';
2
3
  import { pascalCase } from 'putil-varhelpers';
3
4
  import { AsyncEventEmitter } from 'strict-typed-events';
@@ -110,7 +111,7 @@ export class PlatformAdapterHost extends AsyncEventEmitter {
110
111
  return { controller, endpoint, handler };
111
112
  }
112
113
  throw new BadRequestError({
113
- message: translate('ACTION_NOT_FOUND', { resource: resource.name, action: name }, `The {{resource}} resource doesn't have an action named '{{action}}'`),
114
+ message: translate('error:ACTION_NOT_FOUND', { resource: resource.name, action: name }),
114
115
  severity: 'error',
115
116
  code: 'ACTION_NOT_FOUND'
116
117
  });
@@ -127,7 +128,7 @@ export class PlatformAdapterHost extends AsyncEventEmitter {
127
128
  return { controller, endpoint, handler };
128
129
  }
129
130
  throw new ForbiddenError({
130
- message: translate('OPERATION_FORBIDDEN', { resource: resource.name, operation: name }, `'The {{resource}} resource does not accept '{{operation}}' operations`),
131
+ message: translate('error:OPERATION_FORBIDDEN', { resource: resource.name, operation: name }),
131
132
  severity: 'error',
132
133
  code: 'OPERATION_FORBIDDEN'
133
134
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/core",
3
- "version": "0.27.2",
3
+ "version": "0.28.1",
4
4
  "description": "Opra schema package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@browsery/type-is": "^0.6.3",
31
- "@opra/common": "^0.27.2",
31
+ "@opra/common": "^0.28.1",
32
32
  "accepts": "^1.3.8",
33
33
  "content-disposition": "^0.5.4",
34
34
  "content-type": "^1.0.5",
@@ -0,0 +1,7 @@
1
+ declare module "@opra/common" {
2
+ interface I18n {
3
+ loadResourceDir(dirnames: string | string[], deep?: boolean, overwrite?: boolean): Promise<void>;
4
+ loadResourceBundle(lang: string, ns: string, filePath: string, deep?: boolean, overwrite?: boolean): Promise<void>;
5
+ }
6
+ }
7
+ export {};
package/types/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import "reflect-metadata";
2
+ import './augmentation/18n.augmentation.js';
2
3
  import './augmentation/resource.augmentation.js';
3
4
  import './augmentation/collection.augmentation.js';
4
5
  import './augmentation/container.augmentation.js';
@@ -1,3 +1,4 @@
1
+ import './augmentation/18n.augmentation.js';
1
2
  import { AsyncEventEmitter } from 'strict-typed-events';
2
3
  import { Action, ApiDocument, I18n, Operation, Resource } from '@opra/common';
3
4
  import { ExecutionContext } from './execution-context.js';