Skip to content

Commit af8b430

Browse files
authored
refactor(core): enhance class implementations and documentation (#162)
* refactor(core): update class implementations to implement options interfaces - Updated .oxlintrc.json to enable @typescript-eslint/no-unsafe-declaration-merging rule - Updated Api, Field, Operation, and Resource classes to implement their respective options interfaces. - Declare all the fields in the classes instead of relying on interface declaration merging - Update openapi3 test to take new fields order Signed-off-by: J3m5 <[email protected]> * refactor(core): replace assignSealed with Object.assign - Removed assignSealed utility function. - Updated constructors in Api, Field, Operation, and Resource classes to use Object.assign for property assignment. Signed-off-by: J3m5 <[email protected]> * refactor(docs): improve JSDoc comments following oxlint jsdoc plugin - Edit .oxlintrc.json to enable jsdoc plugin - Added parameter and return type annotations in buildEnumObject, getType, fetchJsonLd, parseHydraDocumentation, and handleJson functions. Signed-off-by: J3m5 <[email protected]> * refactor(core): export types from types.js in core index file Signed-off-by: J3m5 <[email protected]> * refactor(package.json): add exports for core module types and implementation Signed-off-by: J3m5 <[email protected]> * refactor(package.json): update lint:fix command and runtime version format - Changed lint:fix command to include --fix-suggestions - Updated runtime version format to include a space Signed-off-by: J3m5 <[email protected]> * refactor(oxlintrc): remove unused ESLint and Unicorn rules Signed-off-by: J3m5 <[email protected]> --------- Signed-off-by: J3m5 <[email protected]>
1 parent c3d087c commit af8b430

File tree

15 files changed

+122
-66
lines changed

15 files changed

+122
-66
lines changed

.oxlintrc.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"import",
2121
"eslint",
2222
"oxc",
23-
"promise"
23+
"promise",
24+
"jsdoc"
2425
],
2526
"rules": {
2627
"@typescript-eslint/no-empty-object-type": [
@@ -30,14 +31,13 @@
3031
}
3132
],
3233
"@typescript-eslint/no-explicit-any": "off",
33-
"@typescript-eslint/no-unsafe-declaration-merging": "off",
34+
"@typescript-eslint/no-unsafe-declaration-merging": "error",
3435
"eslint/arrow-body-style": ["error", "as-needed"],
3536
"eslint/curly": "error",
3637
"eslint/id-length": "off",
3738
"eslint/max-depth": "off",
3839
"eslint/max-lines": "off",
3940
"eslint/max-lines-per-function": "off",
40-
"eslint/max-nested-callbacks": "off",
4141
"eslint/no-duplicate-imports": "off",
4242
"func-style": [
4343
"error",
@@ -90,9 +90,7 @@
9090
}
9191
}
9292
],
93-
"unicorn/no-nested-ternary": "off",
9493
"unicorn/no-null": "off",
95-
"unicorn/number-literal-case": "off",
9694
"vitest/consistent-test-it": [
9795
"error",
9896
{

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
"import": "./lib/index.js",
3131
"default": "./lib/index.js"
3232
},
33+
"./core": {
34+
"types": "./lib/core/index.d.ts",
35+
"import": "./lib/core/index.js",
36+
"default": "./lib/core/index.js"
37+
},
3338
"./package.json": "./package.json"
3439
},
3540
"main": "./lib/index.js",
@@ -47,7 +52,7 @@
4752
"knip": "knip",
4853
"knip:fix": "knip --fix",
4954
"lint": "oxlint",
50-
"lint:fix": "oxlint --fix",
55+
"lint:fix": "oxlint --fix --fix-suggestions",
5156
"test": "vitest run",
5257
"test:coverage": "vitest --coverage",
5358
"test:watch": "vitest --watch",
@@ -86,7 +91,7 @@
8691
},
8792
"runtime": {
8893
"name": "node",
89-
"version": ">=20"
94+
"version": ">= 20"
9095
}
9196
}
9297
}

src/core/Api.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import type { Resource } from "./Resource.js";
22
import type { Nullable } from "./types.js";
3-
import { assignSealed } from "./utils/index.js";
43

54
export interface ApiOptions
65
extends Nullable<{
76
title?: string;
87
resources?: Resource[];
98
}> {}
109

11-
export interface Api extends ApiOptions {}
12-
export class Api {
10+
export class Api implements ApiOptions {
1311
entrypoint: string;
12+
13+
title?: string | null;
14+
resources?: Resource[] | null;
15+
1416
constructor(entrypoint: string, options: ApiOptions = {}) {
1517
this.entrypoint = entrypoint;
16-
assignSealed(this, options);
18+
Object.assign(this, options);
1719
}
1820
}

src/core/Field.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { Resource } from "./Resource.js";
22
import type { Nullable } from "./types.js";
3-
import { assignSealed } from "./utils/index.js";
43

54
export type FieldType =
65
| "string"
@@ -40,18 +39,31 @@ export interface FieldOptions
4039
enum?: { [key: string | number]: string | number };
4140
reference?: string | Resource;
4241
embedded?: Resource;
43-
required?: boolean;
4442
nullable?: boolean;
43+
required?: boolean;
4544
description?: string;
4645
maxCardinality?: number;
4746
deprecated?: boolean;
4847
}> {}
4948

50-
export interface Field extends FieldOptions {}
51-
export class Field {
49+
export class Field implements FieldOptions {
5250
name: string;
51+
52+
id?: string | null;
53+
range?: string | null;
54+
type?: FieldType | null;
55+
arrayType?: FieldType | null;
56+
enum?: { [key: string | number]: string | number } | null;
57+
reference?: string | Resource | null;
58+
embedded?: Resource | null;
59+
nullable?: boolean | null;
60+
required?: boolean | null;
61+
description?: string | null;
62+
maxCardinality?: number | null;
63+
deprecated?: boolean | null;
64+
5365
constructor(name: string, options: FieldOptions = {}) {
5466
this.name = name;
55-
assignSealed(this, options);
67+
Object.assign(this, options);
5668
}
5769
}

src/core/Operation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { Nullable } from "./types.js";
2-
import { assignSealed } from "./utils/index.js";
32

43
export type OperationType = "show" | "edit" | "delete" | "list" | "create";
54

@@ -12,10 +11,16 @@ export interface OperationOptions
1211
deprecated?: boolean;
1312
}> {}
1413

15-
export interface Operation extends OperationOptions {}
16-
export class Operation {
14+
export class Operation implements OperationOptions {
1715
name: string;
1816
type: OperationType;
17+
18+
method?: string | null;
19+
expects?: any | null;
20+
returns?: string | null;
21+
types?: string[] | null;
22+
deprecated?: boolean | null;
23+
1924
constructor(
2025
name: string,
2126
type: OperationType,
@@ -24,6 +29,6 @@ export class Operation {
2429
this.name = name;
2530
this.type = type;
2631

27-
assignSealed(this, options);
32+
Object.assign(this, options);
2833
}
2934
}

src/core/Resource.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,39 @@ import type { Field } from "./Field.js";
22
import type { Operation } from "./Operation.js";
33
import type { Parameter } from "./Parameter.js";
44
import type { Nullable } from "./types.js";
5-
import { assignSealed } from "./utils/index.js";
65

76
export interface ResourceOptions
87
extends Nullable<{
98
id?: string;
109
title?: string;
1110
description?: string;
12-
deprecated?: boolean;
1311
fields?: Field[];
1412
readableFields?: Field[];
1513
writableFields?: Field[];
16-
parameters?: Parameter[];
1714
getParameters?: () => Promise<Parameter[]>;
1815
operations?: Operation[];
16+
deprecated?: boolean;
17+
parameters?: Parameter[];
1918
}> {}
2019

21-
export interface Resource extends ResourceOptions {}
22-
export class Resource {
20+
export class Resource implements ResourceOptions {
2321
name: string;
2422
url: string;
23+
24+
id?: string | null;
25+
title?: string | null;
26+
description?: string | null;
27+
fields?: Field[] | null;
28+
readableFields?: Field[] | null;
29+
writableFields?: Field[] | null;
30+
getParameters?: (() => Promise<Parameter[]>) | null;
31+
operations?: Operation[] | null;
32+
deprecated?: boolean | null;
33+
parameters?: Parameter[] | null;
34+
2535
constructor(name: string, url: string, options: ResourceOptions = {}) {
2636
this.name = name;
2737
this.url = url;
28-
assignSealed(this, options);
38+
Object.assign(this, options);
2939
}
3040
}

src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from "./Field.js";
33
export * from "./Operation.js";
44
export * from "./Parameter.js";
55
export * from "./Resource.js";
6+
export * from "./types.js";

src/core/utils/assignSealed.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/core/utils/buildEnumObject.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { humanize } from "inflection";
55
* The keys of the object are the humanized versions of the enum values,
66
* and the values are the original enum values.
77
*
8-
* @param enumArray - An array of enum values.
9-
* @returns An object mapping humanized enum names to their original values, or null if the input is empty.
8+
* @param {any[] | undefined} enumArray - An array of enum values.
9+
* @returns {Record<string, string | number> | null} An object mapping humanized enum names to their original values, or null if the input is empty.
1010
*/
1111
export function buildEnumObject(
1212
enumArray: any[] | undefined,

src/core/utils/getType.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import type { FieldType } from "../Field.js";
77
* If a format is provided, it will map certain formats (e.g., "int32", "int64") to "integer".
88
* Otherwise, it will camelize the format string. If no format is provided, it returns the OpenAPI type.
99
*
10-
* @param openApiType - The OpenAPI type string.
11-
* @param format - An optional format string.
12-
* @returns The mapped FieldType.
10+
* @param {string} openApiType - The OpenAPI type string.
11+
* @param {string} [format] - An optional format string.
12+
* @returns {FieldType} The mapped FieldType.
1313
*/
1414
export function getType(openApiType: string, format?: string): FieldType {
1515
if (format) {

0 commit comments

Comments
 (0)