@eik/common 4.0.8 → 4.0.9

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [4.0.9](https://github.com/eik-lib/common/compare/v4.0.8...v4.0.9) (2024-08-13)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add jsdoc for schema validators ([#325](https://github.com/eik-lib/common/issues/325)) ([062446a](https://github.com/eik-lib/common/commit/062446a1fa91cf0f1a63078b087a3670815b134c))
7
+
1
8
  ## [4.0.8](https://github.com/eik-lib/common/compare/v4.0.7...v4.0.8) (2024-08-12)
2
9
 
3
10
 
package/README.md CHANGED
@@ -1,149 +1,72 @@
1
- # Eik Common
1
+ # @eik/common
2
2
 
3
- This package contains common utilities and schemas
3
+ This package contains common utilities and schemas used in other Eik modules.
4
4
 
5
- ## ESM ONLY
5
+ ## Schema
6
6
 
7
- From v4, this module is ESM only and cannot be used with Common JS.
7
+ The [schema for `eik.json`](https://eik.dev/docs/reference/eik-json#json-schema)
8
+ can be found here in this repo. Here is how you can use it in your `eik.json`.
8
9
 
9
- ## APIs
10
-
11
- ### schemas
12
-
13
- #### eik.json
14
-
15
- Importing schemas
16
-
17
- ```js
18
- import { schemas, assert } from '@eik/common';
19
- ```
20
-
21
- Validating an `eik.json` file
22
-
23
- ```js
24
- const { error, value } = schemas.validate.eikJSON({
25
- name: 'my-app',
26
- version: '1.0.0',
27
- server: 'http://eik-server',
28
- files: [],
29
- });
30
-
31
- //or
32
-
33
- assert.eikJSON({
34
- name: 'my-app',
35
- version: '1.0.0',
36
- server: 'http://eik-server',
37
- files: [],
38
- });
39
- ```
40
-
41
- Using individual schema validators
42
-
43
- ##### name
44
-
45
- ```js
46
- const { error, value } = schemas.validate.name('my-app');
47
-
48
- // or
49
-
50
- assert.name('my-app');
51
- ```
52
-
53
- ##### version
54
-
55
- ```js
56
- const { error, value } = schemas.validate.version('1.0.0');
57
-
58
- // or
59
-
60
- assert.version('1.0.0');
61
- ```
62
-
63
- ##### type
64
-
65
- ```js
66
- const { error, value } = schemas.validate.type('package');
67
-
68
- // or
69
-
70
- assert.type('package');
10
+ ```json
11
+ {
12
+ "$schema": "https://raw.githubusercontent.com/eik-lib/common/main/lib/schemas/eikjson.schema.json",
13
+ "name": "my-app",
14
+ "version": "1.0.0",
15
+ "server": "https://eik.store.com",
16
+ "files": "./public",
17
+ "import-map": ["https://eik.store.com/map/store/v1"]
18
+ }
71
19
  ```
72
20
 
73
- ##### server
21
+ `@eik/common` has a JavaScript API to [check against the schema](#schemas).
74
22
 
75
- ```js
76
- const { error, value } = schemas.validate.server('http://myeikserver.com');
77
-
78
- // or
23
+ ## API
79
24
 
80
- assert.server('http://myeikserver.com');
81
- ```
25
+ ### helpers
82
26
 
83
- ##### files
27
+ `helpers` has utility functions used by several other Eik modules.
84
28
 
85
29
  ```js
86
- const { error, value } = schemas.validate.files({
87
- './index.js': '/path/to/file.js',
88
- });
89
-
90
- // or
30
+ import { helpers } from '@eik/common';
91
31
 
92
- assert.files({
93
- './index.js': '/path/to/file.js',
94
- });
32
+ let config = helpers.getDefaults();
95
33
  ```
96
34
 
97
- ##### import map
35
+ These are the available functions on `helpers`.
36
+
37
+ | Name | Description |
38
+ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
39
+ | `getDefaults` | Reads configuration from `eik.json` or `package.json`. Includes defaults for missing optional settings. |
40
+ | `localAssets` | Sets up asset routes for local development. Mounted paths match those on Eik server and values are read from projects eik.json file. |
41
+ | `typeSlug` | Maps between Eik configuration values for the package type and its URL/file system value. |
42
+ | `typeTitle` | Maps between a type config value and its title. Essentially uppercases the input. |
43
+ | `addTrailingSlash` | |
44
+ | `removeTrailingSlash` | |
45
+ | `addLeadingSlash` | |
46
+ | `removeLeadingSlash` | |
47
+ | `resolveFiles` | Uses an Eik JSON "files" definition to resolve files on disk into a data structure. Returns a list of [ResolvedFile](https://github.com/eik-lib/common/blob/main/lib/classes/resolved-files.js). |
48
+ | `configStore` | Collection of helper methods for reading and writing Eik configuration files. |
98
49
 
99
- ```js
100
- const { error, value } = schemas.validate.importMap(
101
- 'http://meserver.com/map.json',
102
- );
103
-
104
- const { error, value } = schemas.validate.importMap([
105
- 'http://meserver.com/map1.json',
106
- 'http://meserver.com/map2.json',
107
- ]);
108
-
109
- // or
50
+ #### localAssets
110
51
 
111
- assert.importMap([
112
- 'http://meserver.com/map1.json',
113
- 'http://meserver.com/map2.json',
114
- ]);
115
- ```
52
+ Sets up asset routes for local development. Mounted paths match those on Eik server and values are read from projects eik.json file.
116
53
 
117
- ##### out
54
+ Given this server and `eik.json`, the following routes would be added to your app.
118
55
 
119
56
  ```js
120
- const { error, value } = schemas.validate.out('./.eik');
121
-
122
- // or
123
-
124
- assert.out('./.eik');
125
- ```
126
-
127
- ### helpers
128
-
129
- #### localAssets
57
+ import { helpers } from '@eik/common';
58
+ import express from 'express';
130
59
 
131
- A function to help development by mounting development routes to an Express.js or Fastify app based on values defined in `eik.json`
60
+ let app = express();
132
61
 
133
- ```js
134
- import express from 'express';
135
- import { helpers } from '@eik/common';
136
- const app = express();
137
62
  await helpers.localAssets(app);
138
63
  ```
139
64
 
140
- For an `eik.json` file such as
141
-
142
65
  ```json
143
66
  {
144
67
  "name": "my-app",
145
68
  "version": "1.0.0",
146
- "server": "https://assets.myeikserver.com",
69
+ "server": "https://eik.store.com",
147
70
  "files": {
148
71
  "esm.js": "./assets/esm.js",
149
72
  "esm.css": "./assets/esm.css",
@@ -152,8 +75,6 @@ For an `eik.json` file such as
152
75
  }
153
76
  ```
154
77
 
155
- A number of routes would be mounted into your app.
156
-
157
78
  ```
158
79
  /pkg/my-app/1.0.0/esm.js
159
80
  /pkg/my-app/1.0.0/esm.css
@@ -161,30 +82,81 @@ A number of routes would be mounted into your app.
161
82
  /pkg/my-app/1.0.0/esm.css.map
162
83
  ```
163
84
 
164
- #### packageURL
85
+ ### schemas
165
86
 
166
- This helper function can be used to build URLs for given entries in an `eik.json` files section.
87
+ `schemas` has functions to check values against the [`eik.json` schema](#schema).
88
+ You can check a value against the schema for `eik.json` as a whole, or for individual
89
+ values in the schema.
167
90
 
168
- Given the following `eik.json` file:
91
+ ```js
92
+ import { schemas } from '@eik/common';
169
93
 
170
- ```json
171
- {
172
- "name": "my-app",
173
- "version": "1.0.0",
174
- "server": "https://assets.myeikserver.com",
175
- "files": {
176
- "esm.js": "./assets/esm.js",
177
- "esm.css": "./assets/esm.css",
178
- "/": "./assets/**/*.map"
179
- }
94
+ let { error, value } = schemas.validate.eikJSON(eikConfig);
95
+ if (error) {
96
+ // fallback
180
97
  }
181
98
  ```
182
99
 
183
- and the following call to packageURL
100
+ If you prefer, you can use the `assert` API which throws on error.
184
101
 
185
102
  ```js
186
- import { helpers } from '@eik/common';
187
- const url = await helpers.packageURL('esm.js');
103
+ import { schemas } from '@eik/common';
104
+
105
+ try {
106
+ schemas.assert.eikJSON(eikConfig);
107
+ } catch {
108
+ // fallback
109
+ }
110
+ ```
111
+
112
+ These are the available functions on `schemas.validate` and `schemas.assert`.
113
+
114
+ | Name | Description |
115
+ | ----------- | ----------------------------------------------------------------------- |
116
+ | `eikJSON` | Checks that the given value includes required fields that are valid |
117
+ | `name` | Checks [name](https://eik.dev/docs/reference/eik-json#name) |
118
+ | `version` | Checks [version](https://eik.dev/docs/reference/eik-json#version) |
119
+ | `type` | Checks [type](https://eik.dev/docs/reference/eik-json#type) |
120
+ | `server` | Checks [server](https://eik.dev/docs/reference/eik-json#server) |
121
+ | `files` | Checks [files](https://eik.dev/docs/reference/eik-json#files) |
122
+ | `importMap` | Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map) |
123
+ | `out` | Checks [out](https://eik.dev/docs/reference/eik-json#out) |
124
+
125
+ ### stream
126
+
127
+ `stream` has functions to check that a value is a Stream.
128
+
129
+ ```js
130
+ import { stream } from '@eik/common';
131
+
132
+ if (stream.isStream(maybeStream)) {
133
+ // yup, it's a Stream
134
+ }
135
+
136
+ if (stream.isReadableStream(maybeReadableStream)) {
137
+ // yup, it's a ReadableStream
138
+ }
139
+ ```
140
+
141
+ ### validators
142
+
143
+ `validators` functions return the provided string normalized to lowercase, or throw an Error if the value does not pass the validation rules.
144
+ Where possible, prefer using the [`schemas` API](#schemas).
145
+
146
+ ```js
147
+ import { validators } from '@eik/common';
148
+
149
+ let alias = validators.alias('1');
188
150
  ```
189
151
 
190
- The URL returned will be `https://assets.myeikserver.com/pkg/my-app/1.0.0/esm.js`
152
+ These are the available functions on `validators`.
153
+
154
+ | Name | Description |
155
+ | ------------ | ---------------------------------------------------------------- |
156
+ | `alias` | Checks that a value is a valid alias value (ex 1) |
157
+ | `name` | Checks that a value is a valid package name |
158
+ | `org` | Checks that a value is a valid organisation name. |
159
+ | `origin` | Check that a value looks like an HTTP origin. |
160
+ | `version` | Checks that a value is a valid semver version |
161
+ | `semverType` | Checks that a value is a valid semver type (major, minor, patch) |
162
+ | `type` | Checks that the value is a valid Eik type (pkg, npm, map) |
@@ -1,7 +1,8 @@
1
- // @ts-check
2
-
3
1
  /**
4
- * @type {(type: string) => string}
2
+ * Maps between Eik configuration values for the package type and its URL/file system value.
3
+ * Essentially `package` -> `pkg`.
4
+ * @param {string} type
5
+ * @returns {string} the Eik package type
5
6
  */
6
7
  export default (type) => {
7
8
  if (type === 'package') return 'pkg';
@@ -1,7 +1,7 @@
1
- // @ts-check
2
-
3
1
  /**
4
- * @type {(type: string) => string}
2
+ * Maps between a type config value and its title. Essentially uppercases the input.
3
+ * @param {string} type
4
+ * @param {"PACKAGE" | "NPM" | "MAP"} type
5
5
  */
6
6
  export default (type) => {
7
7
  if (type === 'package') return 'PACKAGE';
@@ -1,4 +1,3 @@
1
- // @ts-check
2
1
  import {
3
2
  eikJSON,
4
3
  name,
@@ -11,7 +10,20 @@ import {
11
10
  } from './validate.js';
12
11
  import ValidationError from './validation-error.js';
13
12
 
14
- // @ts-ignore
13
+ /**
14
+ * @template T
15
+ * @callback SchemaAssert
16
+ * @param {T} value
17
+ * @returns {void}
18
+ * @throws {ValidationError} if validation fails
19
+ */
20
+
21
+ /**
22
+ * @template T
23
+ * @param {import('./validate.js').SchemaValidator<T>} validate
24
+ * @param {string} message
25
+ * @returns {SchemaAssert<T>}
26
+ */
15
27
  const assert = (validate, message) => (value) => {
16
28
  const valid = validate(value);
17
29
  if (valid.error) {
@@ -25,17 +37,26 @@ const assert = (validate, message) => (value) => {
25
37
  return msg;
26
38
  })
27
39
  .join(',');
40
+ // @ts-expect-error Maybe some toString magic happens here?
28
41
  throw new ValidationError(`${message}: ${errorMessage}`, valid.error);
29
42
  }
30
43
  };
31
44
 
32
45
  export default {
46
+ /** Asserts the given [eik.json](https://eik.dev/docs/reference/eik-json) includes required fields that are valid */
33
47
  eikJSON: assert(eikJSON, 'Invalid eik.json schema'),
48
+ /** Asserts the given [name](https://eik.dev/docs/reference/eik-json#name) value is valid*/
34
49
  name: assert(name, 'Parameter "name" is not valid'),
50
+ /** Asserts the given [type](https://eik.dev/docs/reference/eik-json#type) value is valid*/
35
51
  type: assert(type, 'Parameter "type" is not valid'),
52
+ /** Asserts the given [version](https://eik.dev/docs/reference/eik-json#version) value is valid*/
36
53
  version: assert(version, 'Parameter "version" is not valid'),
54
+ /** Asserts the given [server](https://eik.dev/docs/reference/eik-json#server) value is valid*/
37
55
  server: assert(server, 'Parameter "server" is not valid'),
56
+ /** Asserts the given [files](https://eik.dev/docs/reference/eik-json#files) value is valid */
38
57
  files: assert(files, 'Parameter "files" is not valid'),
58
+ /** Asserts the given [import-map](https://eik.dev/docs/reference/eik-json#import-map) value is valid */
39
59
  importMap: assert(importMap, 'Parameter "import-map" is not valid'),
60
+ /** Asserts the given [out](https://eik.dev/docs/reference/eik-json#out) value is valid */
40
61
  out: assert(out, 'Parameter "out" is not valid'),
41
62
  };
@@ -13,7 +13,24 @@ const eikJSONSchema = JSON.parse(
13
13
  readFileSync(join(__dirname, './eikjson.schema.json'), 'utf8'),
14
14
  );
15
15
 
16
- // @ts-ignore
16
+ /**
17
+ * @typedef {import('ajv').ErrorObject & { dataPath?: string }} ErrorObject
18
+ */
19
+
20
+ /**
21
+ * @template [T=unknown]
22
+ * @callback SchemaValidator
23
+ * @param {T} data
24
+ * @returns {{ value: T; error: false | import('ajv').ErrorObject[]}}
25
+ */
26
+
27
+ /**
28
+ * Create a validator for the schema using {@link Ajv}
29
+ * @template [T=unknown]
30
+ * @param {unknown} schema
31
+ * @param {import('ajv').Options} [ajvOptions]
32
+ * @returns {SchemaValidator<T>}
33
+ */
17
34
  const createValidator = (schema, ajvOptions) => {
18
35
  // @ts-ignore
19
36
  const ajv = new Ajv(ajvOptions);
@@ -21,7 +38,6 @@ const createValidator = (schema, ajvOptions) => {
21
38
  formats(ajv); // Needed to support "uri"
22
39
  const validate = ajv.compile(schema);
23
40
 
24
- // @ts-ignore
25
41
  return (data) => {
26
42
  const cloned = JSON.parse(JSON.stringify(data));
27
43
  const valid = validate(cloned);
@@ -29,20 +45,30 @@ const createValidator = (schema, ajvOptions) => {
29
45
  };
30
46
  };
31
47
 
48
+ /**
49
+ * Checks that the given value includes required fields that are valid
50
+ * @type {SchemaValidator<import('../../eikjson.js').EikjsonSchema>}
51
+ */
32
52
  const eikJSON = createValidator(eikJSONSchema, {
33
53
  removeAdditional: true,
34
54
  useDefaults: true,
35
55
  });
36
- // @ts-ignore
56
+
57
+ /**
58
+ * @param {SchemaValidator<string>} jsonSchemaValidator
59
+ * @returns {SchemaValidator<string>}
60
+ */
37
61
  const createNameValidator = (jsonSchemaValidator) => (value) => {
38
62
  const result = jsonSchemaValidator(value);
39
63
  if (!result.error) {
40
64
  const pkvalid = npmPkg(value);
65
+ /** @type {ErrorObject[]} */
41
66
  const errors = [];
42
67
  if (!pkvalid.validForNewPackages) {
43
68
  errors.push({
44
69
  keyword: 'validForNewPackages',
45
- dataPath: '.name',
70
+ instancePath: '.name',
71
+ dataPath: '.name', // this was here before, but maybe replaced by instancePath?
46
72
  schemaPath: '',
47
73
  params: [],
48
74
  message: 'should be valid package name',
@@ -54,15 +80,21 @@ const createNameValidator = (jsonSchemaValidator) => (value) => {
54
80
  }
55
81
  return result;
56
82
  };
57
- // @ts-ignore
83
+
84
+ /**
85
+ * @param {SchemaValidator<string>} jsonSchemaValidator
86
+ * @returns {SchemaValidator<string>}
87
+ */
58
88
  const createVersionValidator = (jsonSchemaValidator) => (value) => {
59
89
  const result = jsonSchemaValidator(value);
60
90
  if (!result.error) {
61
91
  const version = semver.valid(value);
92
+ /** @type {ErrorObject[]} */
62
93
  const errors = [];
63
94
  if (!version) {
64
95
  errors.push({
65
96
  keyword: 'invalidSemverRange',
97
+ instancePath: '.version',
66
98
  dataPath: '.version',
67
99
  schemaPath: '',
68
100
  params: [],
@@ -76,16 +108,50 @@ const createVersionValidator = (jsonSchemaValidator) => (value) => {
76
108
  return result;
77
109
  };
78
110
 
111
+ /**
112
+ * Checks [name](https://eik.dev/docs/reference/eik-json#name)
113
+ * @type {SchemaValidator<string>}
114
+ */
79
115
  const name = createNameValidator(
80
116
  createValidator(eikJSONSchema.properties.name),
81
117
  );
118
+
119
+ /**
120
+ * Checks [version](https://eik.dev/docs/reference/eik-json#version)
121
+ * @type {SchemaValidator<string>}
122
+ */
82
123
  const version = createVersionValidator(
83
124
  createValidator(eikJSONSchema.properties.version),
84
125
  );
126
+
127
+ /**
128
+ * Checks [type](https://eik.dev/docs/reference/eik-json#type)
129
+ * @type {SchemaValidator<string>}
130
+ */
85
131
  const type = createValidator(eikJSONSchema.properties.type);
132
+
133
+ /**
134
+ * Checks [server](https://eik.dev/docs/reference/eik-json#server)
135
+ * @type {SchemaValidator<string>}
136
+ */
86
137
  const server = createValidator(eikJSONSchema.properties.server);
138
+
139
+ /**
140
+ * Checks [files](https://eik.dev/docs/reference/eik-json#files)
141
+ * @type {SchemaValidator<string | Record<string, string>>}
142
+ */
87
143
  const files = createValidator(eikJSONSchema.properties.files);
144
+
145
+ /**
146
+ * Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map)
147
+ * @type {SchemaValidator<string | string[]>}
148
+ */
88
149
  const importMap = createValidator(eikJSONSchema.properties['import-map']);
150
+
151
+ /**
152
+ * Checks [out](https://eik.dev/docs/reference/eik-json#out)
153
+ * @type {SchemaValidator<string>}
154
+ */
89
155
  const out = createValidator(eikJSONSchema.properties.out);
90
156
 
91
157
  export { eikJSON, name, version, type, server, files, importMap, out };
package/lib/stream.js CHANGED
@@ -1,12 +1,24 @@
1
- // @ts-ignore
1
+ /**
2
+ * Checks if the value is a Stream
3
+ * @param {unknown} stream
4
+ * @returns {boolean}
5
+ */
2
6
  export const isStream = (stream) =>
3
7
  stream !== null &&
4
8
  typeof stream === 'object' &&
9
+ // @ts-expect-error
5
10
  typeof stream.pipe === 'function';
6
11
 
7
- // @ts-ignore
12
+ /**
13
+ * Checks if the value is a {@link ReadableStream}
14
+ * @param {unknown} stream
15
+ * @returns {boolean}
16
+ */
8
17
  export const isReadableStream = (stream) =>
9
18
  isStream(stream) &&
19
+ // @ts-expect-error
10
20
  stream.readable !== false &&
21
+ // @ts-expect-error
11
22
  typeof stream._read === 'function' &&
23
+ // @ts-expect-error
12
24
  typeof stream._readableState === 'object';
@@ -2,7 +2,13 @@ import semver from 'semver';
2
2
  import npmPkg from 'validate-npm-package-name';
3
3
 
4
4
  const urlIsh = /^https?:\/\/[a-zA-Z0-9-_./]+(:[0-9]+)?/;
5
- // @ts-ignore
5
+
6
+ /**
7
+ * Check that a value looks like an HTTP origin.
8
+ * @param {string} value
9
+ * @returns {string} the value in lowercase
10
+ * @throws {Error} if the parameter does not match a URL pattern
11
+ */
6
12
  export const origin = (value) => {
7
13
  if (urlIsh.test(value)) {
8
14
  return value.toLowerCase();
@@ -10,7 +16,12 @@ export const origin = (value) => {
10
16
  throw new Error('Parameter "origin" is not valid');
11
17
  };
12
18
 
13
- // @ts-ignore
19
+ /**
20
+ * Checks that a value is a valid organisation name.
21
+ * @param {string} value
22
+ * @returns {string} the value in lowercase
23
+ * @throws {Error} if the value is not a valid organisation name
24
+ */
14
25
  export const org = (value) => {
15
26
  if (/^[a-zA-Z0-9_-]+$/.test(value)) {
16
27
  return value.toLowerCase();
@@ -18,7 +29,12 @@ export const org = (value) => {
18
29
  throw new Error(`Parameter "org" is not valid - Value: ${value}`);
19
30
  };
20
31
 
21
- // @ts-ignore
32
+ /**
33
+ * Checks that a value is a valid package name.
34
+ * @param {string} value
35
+ * @returns {string} the value in lowercase
36
+ * @throws {Error} if the value is not a valid package name
37
+ */
22
38
  export const name = (value) => {
23
39
  const result = npmPkg(value);
24
40
  if (result.validForNewPackages || result.validForOldPackages) {
@@ -27,7 +43,12 @@ export const name = (value) => {
27
43
  throw new Error(`Parameter "name" is not valid - Value: ${value}`);
28
44
  };
29
45
 
30
- // @ts-ignore
46
+ /**
47
+ * Checks that a value is a valid semver version.
48
+ * @param {string} value
49
+ * @returns {string} the value in lowercase
50
+ * @throws {Error} if the value is not a valid semver version
51
+ */
31
52
  export const version = (value) => {
32
53
  const result = semver.valid(value);
33
54
  if (result) {
@@ -36,7 +57,12 @@ export const version = (value) => {
36
57
  throw new Error(`Parameter "version" is not valid - Value: ${value}`);
37
58
  };
38
59
 
39
- // @ts-ignore
60
+ /**
61
+ * Checks that a value is a valid alias value.
62
+ * @param {string} value
63
+ * @returns {string} the value in lowercase
64
+ * @throws {Error} if the value is not a valid alias value
65
+ */
40
66
  export const alias = (value) => {
41
67
  if (/^[0-9]+$/.test(value)) {
42
68
  return value;
@@ -44,7 +70,12 @@ export const alias = (value) => {
44
70
  throw new Error(`Parameter "alias" is not valid - Value: ${value}`);
45
71
  };
46
72
 
47
- // @ts-ignore
73
+ /**
74
+ * Checks that a value is a valid Eik package type.
75
+ * @param {string} value
76
+ * @returns {string} the value in lowercase
77
+ * @throws {Error} if the value is not a valid Eik package type
78
+ */
48
79
  export const type = (value) => {
49
80
  if (value === 'pkg' || value === 'map' || value === 'npm') {
50
81
  return value;
@@ -52,11 +83,21 @@ export const type = (value) => {
52
83
  throw new Error(`Parameter "type" is not valid - Value: ${value}`);
53
84
  };
54
85
 
55
- // TODO; https://github.com/asset-pipe/core/issues/12
56
- // @ts-ignore
86
+ /**
87
+ * No-op, returning its value.
88
+ * @param {string} value
89
+ * @returns {string} the value
90
+ * @todo https://github.com/asset-pipe/core/issues/12
91
+ * @todo Can we remove this?
92
+ */
57
93
  export const extra = (value) => value;
58
94
 
59
- // @ts-ignore
95
+ /**
96
+ * Checks that a value is a valid semver type (major, minor, patch).
97
+ * @param {string} value
98
+ * @returns {string} the value
99
+ * @throws {Error} if the value is not a valid semver type
100
+ */
60
101
  export const semverType = (value) => {
61
102
  if (value === 'major' || value === 'minor' || value === 'patch') {
62
103
  return value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eik/common",
3
- "version": "4.0.8",
3
+ "version": "4.0.9",
4
4
  "description": "Common utilities for Eik modules",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",
@@ -1,2 +1,2 @@
1
- declare const _default: (type: string) => string;
1
+ declare function _default(type: string): string;
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: (type: string) => string;
1
+ declare function _default(type: string): "PACKAGE" | "NPM" | "MAP";
2
2
  export default _default;
@@ -1,11 +1,12 @@
1
1
  declare namespace _default {
2
- function eikJSON(value: any): void;
3
- function name(value: any): void;
4
- function type(value: any): void;
5
- function version(value: any): void;
6
- function server(value: any): void;
7
- function files(value: any): void;
8
- function importMap(value: any): void;
9
- function out(value: any): void;
2
+ function eikJSON(value: import("../../eikjson.js").EikjsonSchema): void;
3
+ function name(value: string): void;
4
+ function type(value: string): void;
5
+ function version(value: string): void;
6
+ function server(value: string): void;
7
+ function files(value: string | Record<string, string>): void;
8
+ function importMap(value: string | string[]): void;
9
+ function out(value: string): void;
10
10
  }
11
11
  export default _default;
12
+ export type SchemaAssert<T> = (value: T) => void;
@@ -1,26 +1,47 @@
1
- export function eikJSON(data: any): {
2
- value: any;
3
- error: any;
1
+ export type ErrorObject = import("ajv").ErrorObject & {
2
+ dataPath?: string;
4
3
  };
5
- export function name(value: any): any;
6
- export function version(value: any): any;
7
- export function type(data: any): {
8
- value: any;
9
- error: any;
10
- };
11
- export function server(data: any): {
12
- value: any;
13
- error: any;
14
- };
15
- export function files(data: any): {
16
- value: any;
17
- error: any;
18
- };
19
- export function importMap(data: any): {
20
- value: any;
21
- error: any;
22
- };
23
- export function out(data: any): {
24
- value: any;
25
- error: any;
4
+ export type SchemaValidator<T = unknown> = (data: T) => {
5
+ value: T;
6
+ error: false | import("ajv").ErrorObject[];
26
7
  };
8
+ /**
9
+ * Checks that the given value includes required fields that are valid
10
+ * @type {SchemaValidator<import('../../eikjson.js').EikjsonSchema>}
11
+ */
12
+ export const eikJSON: SchemaValidator<import("../../eikjson.js").EikjsonSchema>;
13
+ /**
14
+ * Checks [name](https://eik.dev/docs/reference/eik-json#name)
15
+ * @type {SchemaValidator<string>}
16
+ */
17
+ export const name: SchemaValidator<string>;
18
+ /**
19
+ * Checks [version](https://eik.dev/docs/reference/eik-json#version)
20
+ * @type {SchemaValidator<string>}
21
+ */
22
+ export const version: SchemaValidator<string>;
23
+ /**
24
+ * Checks [type](https://eik.dev/docs/reference/eik-json#type)
25
+ * @type {SchemaValidator<string>}
26
+ */
27
+ export const type: SchemaValidator<string>;
28
+ /**
29
+ * Checks [server](https://eik.dev/docs/reference/eik-json#server)
30
+ * @type {SchemaValidator<string>}
31
+ */
32
+ export const server: SchemaValidator<string>;
33
+ /**
34
+ * Checks [files](https://eik.dev/docs/reference/eik-json#files)
35
+ * @type {SchemaValidator<string | Record<string, string>>}
36
+ */
37
+ export const files: SchemaValidator<string | Record<string, string>>;
38
+ /**
39
+ * Checks [import-map](https://eik.dev/docs/reference/eik-json#import-map)
40
+ * @type {SchemaValidator<string | string[]>}
41
+ */
42
+ export const importMap: SchemaValidator<string | string[]>;
43
+ /**
44
+ * Checks [out](https://eik.dev/docs/reference/eik-json#out)
45
+ * @type {SchemaValidator<string>}
46
+ */
47
+ export const out: SchemaValidator<string>;
package/types/stream.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export function isStream(stream: any): boolean;
2
- export function isReadableStream(stream: any): boolean;
1
+ export function isStream(stream: unknown): boolean;
2
+ export function isReadableStream(stream: unknown): boolean;
@@ -1,8 +1,8 @@
1
- export function origin(value: any): any;
2
- export function org(value: any): any;
3
- export function name(value: any): any;
4
- export function version(value: any): string;
5
- export function alias(value: any): any;
6
- export function type(value: any): any;
7
- export function extra(value: any): any;
8
- export function semverType(value: any): any;
1
+ export function origin(value: string): string;
2
+ export function org(value: string): string;
3
+ export function name(value: string): string;
4
+ export function version(value: string): string;
5
+ export function alias(value: string): string;
6
+ export function type(value: string): string;
7
+ export function extra(value: string): string;
8
+ export function semverType(value: string): string;