@pikku/schema-cfworker 0.6.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/CHANGELOG.md ADDED
@@ -0,0 +1,17 @@
1
+ # @pikku/cfworker-json-schema
2
+
3
+ ## 0.6.2
4
+
5
+ ### Patch Changes
6
+
7
+ - e0dd19a: fix: invalid schemas should result in a 422
8
+ - Updated dependencies [e0dd19a]
9
+ - @pikku/core@0.6.12
10
+
11
+ ## 0.6.1
12
+
13
+ ### Patch Changes
14
+
15
+ - 0a92fa7: refactor: pulling schema into seperate package since ajv doesnt work on cloudflare (also keeps bundle size small!)
16
+ - Updated dependencies [0a92fa7]
17
+ - @pikku/core@0.6.7
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # @pikku/cfworker-schema
2
+
3
+ This package is part of @pikku/mono and is responsible for providing the express-middleware to enable pikku routes
@@ -0,0 +1,9 @@
1
+ import { Logger, SchemaService } from '@pikku/core';
2
+ export declare class CFWorkerSchemaService implements SchemaService {
3
+ private logger;
4
+ private validators;
5
+ constructor(logger: Logger);
6
+ compileSchema(schema: string, value: any): void;
7
+ validateSchema(schemaName: string, json: any): void;
8
+ getSchemaNames(): Set<string>;
9
+ }
@@ -0,0 +1,35 @@
1
+ import { UnprocessableContentError } from '@pikku/core';
2
+ import { Validator } from '@cfworker/json-schema';
3
+ export class CFWorkerSchemaService {
4
+ logger;
5
+ validators = new Map();
6
+ constructor(logger) {
7
+ this.logger = logger;
8
+ }
9
+ compileSchema(schema, value) {
10
+ if (!this.validators.has(schema)) {
11
+ this.logger.debug(`Adding json schema for ${schema}`);
12
+ try {
13
+ const validator = new Validator(value);
14
+ this.validators.set(schema, validator);
15
+ }
16
+ catch (e) {
17
+ throw e;
18
+ }
19
+ }
20
+ }
21
+ validateSchema(schemaName, json) {
22
+ const validator = this.validators.get(schemaName);
23
+ if (!validator) {
24
+ throw `Missing validator for ${schemaName}`;
25
+ }
26
+ const result = validator.validate(json);
27
+ if (!result.valid) {
28
+ this.logger.error(`failed to validate request data against schema '${schemaName}'`, json, result.errors);
29
+ throw new UnprocessableContentError(result.errors.join(', '));
30
+ }
31
+ }
32
+ getSchemaNames() {
33
+ return new Set(this.validators.keys());
34
+ }
35
+ }
@@ -0,0 +1 @@
1
+ export * from './cfworker-json-schema.js';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './cfworker-json-schema.js';
package/package.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "name": "@pikku/schema-cfworker",
3
+ "version": "0.6.0",
4
+ "description": "The pikku schema cfworker-json-schema package",
5
+ "license": "MIT",
6
+ "author": "yasser.fadl@gmail.com",
7
+ "module": "dist/index.js",
8
+ "main": "dist/index.js",
9
+ "type": "module",
10
+ "scripts": {
11
+ "tsc": "tsc",
12
+ "ncu": "npx npm-check-updates",
13
+ "build:esm": "tsc -b",
14
+ "build": "yarn build:esm",
15
+ "release": "npm run build && npm test",
16
+ "test": "bash run-tests.sh",
17
+ "test:watch": "bash run-tests.sh --watch",
18
+ "test:coverage": "bash run-tests.sh --coverage"
19
+ },
20
+ "peerDependencies": {
21
+ "@pikku/core": "^0.6.12"
22
+ },
23
+ "devDependencies": {
24
+ "typescript": "^5.6.3"
25
+ },
26
+ "engines": {
27
+ "node": ">=18"
28
+ },
29
+ "dependencies": {
30
+ "@cfworker/json-schema": "^4.1.1"
31
+ }
32
+ }
package/run-tests.sh ADDED
@@ -0,0 +1,53 @@
1
+ #!/bin/bash
2
+
3
+ # Enable nullglob to handle cases where no files match the pattern
4
+ shopt -s nullglob
5
+
6
+ # Initialize variables for options
7
+ watch_mode=false
8
+ coverage_mode=false
9
+
10
+ # Parse command-line options
11
+ while [[ $# -gt 0 ]]; do
12
+ case $1 in
13
+ --watch)
14
+ watch_mode=true
15
+ shift
16
+ ;;
17
+ --coverage)
18
+ coverage_mode=true
19
+ shift
20
+ ;;
21
+ *)
22
+ echo "Unknown option: $1"
23
+ exit 1
24
+ ;;
25
+ esac
26
+ done
27
+
28
+ # Define the pattern to match your test files
29
+ pattern="src/*.test.ts"
30
+
31
+ # Expand the pattern into an array of files
32
+ files=($(find src -type f -name "*.test.ts"))
33
+
34
+ # Check if any files matched the pattern
35
+ if [ ${#files[@]} -eq 0 ]; then
36
+ echo "No test files found matching pattern: $pattern"
37
+ exit 0
38
+ fi
39
+
40
+ # Construct the node command
41
+ node_cmd="node --import tsx --test"
42
+
43
+ # Append options based on flags
44
+ if [ "$watch_mode" = true ]; then
45
+ node_cmd="$node_cmd --watch"
46
+ fi
47
+
48
+ if [ "$coverage_mode" = true ]; then
49
+ node_cmd="$node_cmd --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=lcov.info"
50
+ fi
51
+
52
+ # Execute the node command with the expanded list of files
53
+ $node_cmd "${files[@]}"