Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to tc39 decorators #95

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [2.0.0](https://github.com/Netatwork-de/odata-edm-generator/compare/v1.0.2...v2.0.0) (2024-04-29)

### [1.0.2](https://github.com/Netatwork-de/odata-edm-generator/compare/v1.0.0...v1.0.2) (2024-03-12)

## [1.0.0](https://github.com/Netatwork-de/odata-edm-generator/compare/v1.0.0-7...v1.0.0) (2024-03-08)
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@netatwork/odata-edm-generator",
"version": "1.0.2",
"version": "2.0.0",
"description": "OData EDM generator",
"type": "module",
"exports": {
Expand Down Expand Up @@ -62,7 +62,7 @@
"rimraf": "^5.0.1",
"standard-version": "^9.5.0",
"ts-node": "^10.9.1",
"typescript": "^5.4.2",
"typescript": "^5.4.5",
"uuid": "^9.0.0"
},
"dependencies": {
Expand Down
65 changes: 36 additions & 29 deletions src/api/decorators.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-explicit-any */

export type ODataEndpointDecorator = <T extends Class<any>>(target: T, context: ClassDecoratorContext<T>) => void;

/**
* Decorator to associate an endpoint with an odata entity.
*/
export function odataEndpoint(endpoint: string) {
return function(constructor: Class<any>): void {
Reflect.defineProperty(constructor, 'ODataEndpoint', { configurable: true, writable: false, enumerable: false, value: endpoint });
};
export function odataEndpoint(endpoint: string): ODataEndpointDecorator {
return function<T extends Class<any>>(target: T, context: ClassDecoratorContext<T>): void {
context.addInitializer(function() {
Reflect.defineProperty(this, 'ODataEndpoint', { configurable: true, writable: false, enumerable: false, value: endpoint });
});
};
}

export type Class<T> = {
readonly prototype: T;
new(...args: any[]): T;
readonly prototype: T;
new(...args: any[]): T;
};
export type ODataEntity<T> = Class<T> & {
ODataEndpoint: string;
ODataEndpoint: string;
};

export const odataTypeKey = '@odata.type' as const;
export type ODataComplexType<T> = Class<T> & {
readonly prototype: T & { [odataTypeKey]: string };
canHandle(arg: string): boolean;
readonly prototype: T & { [odataTypeKey]: string };
canHandle(arg: string): boolean;
};

export type ODataTypeDecorator = <T extends Class<any>>(target: T, context: ClassDecoratorContext<T>) => T;

/**
* This is similar to `@odataEndpoint`, but this is more useful for interfaces and abstract class hierarchy in odata.
* It does as follows:
Expand All @@ -31,25 +38,25 @@ export type ODataComplexType<T> = Class<T> & {
* - Adds a static method to the class named `canHandle`, which when given a `@odata.type` value (string) returns `true`/`false`
* depending on whether the value matches the given `rawOdataType` or not. This is useful for factory methods.
*/
export function odataType(rawOdataType: string): any;
export function odataType(rawOdataType: string, friendlyType: string, typePropertyName: string): any;
export function odataType(rawOdataType: string, friendlyType?: string, typePropertyName?: string): any {

return function <T extends new (...args: any[]) => any>(constructorFunction: T) {
if (!rawOdataType) {
throw new Error(`Cannot define odataType on ${constructorFunction.name}, as missing rawOdataType`);
}
Reflect.defineProperty(constructorFunction, 'canHandle', { get() { return (arg: string) => arg === rawOdataType; } });
if (friendlyType && typePropertyName) {
Reflect.set(constructorFunction.prototype as object, typePropertyName, friendlyType);
}

return class extends constructorFunction {
public constructor(...args: any[]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
super(...args);
Reflect.set(this, odataTypeKey, rawOdataType);
}
};
export function odataType(rawOdataType: string): ODataTypeDecorator;
export function odataType(rawOdataType: string, friendlyType: string, typePropertyName: string): ODataTypeDecorator;
export function odataType(rawOdataType: string, friendlyType?: string, typePropertyName?: string): ODataTypeDecorator {
return function<T extends Class<any>>(target: T, context: ClassDecoratorContext<T>) {
if (!rawOdataType) {
throw new Error(`Cannot define odataType on ${target.name}, as missing rawOdataType`);
}
context.addInitializer(function () {
Reflect.defineProperty(this, 'canHandle', { get() { return (arg: string) => arg === rawOdataType; } });
if (friendlyType && typePropertyName) {
Reflect.set(this.prototype as object, typePropertyName, friendlyType);
}
});
return class extends target {
public constructor(...args: any[]) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
super(...args);
Reflect.set(this, odataTypeKey, rawOdataType);
}
};
};
}
22 changes: 22 additions & 0 deletions tests/decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { assert } from 'chai';
import { ODataComplexType, ODataEntity, odataEndpoint, odataType } from '../src/api/decorators.js';

describe('decorators', function () {
it('odataEndpoint', function () {
@odataEndpoint('test')
class Test {}

assert.strictEqual((Test as ODataEntity<unknown>).ODataEndpoint, 'test');
});

it('odataType', function () {
@odataType('rawTestType', 'friendlyTestType', 'testType')
class Test {}
const instance = new Test();
assert.strictEqual((Test as ODataComplexType<unknown>).canHandle('wrongType'), false);
assert.strictEqual((Test as ODataComplexType<unknown>).canHandle('rawTestType'), true);
assert.strictEqual((instance as { '@odata.type': string })['@odata.type'], 'rawTestType');
assert.strictEqual((Test.prototype as { testType: string }).testType, 'friendlyTestType');
assert.strictEqual((instance as { testType: string }).testType, 'friendlyTestType');
});
});
12 changes: 4 additions & 8 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ES2020",
"lib": [
"dom"
],
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Node",
"declaration": true,
"declarationMap": true,
"declarationDir": "./dist/types",
"sourceMap": true,
"outDir": "./dist/esm",
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"stripInternal": true,
"removeComments": true,
"incremental": true,
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"downlevelIteration": true
"tsBuildInfoFile": "./dist/.tsbuildinfo"
},
"include": [
"./src"
Expand Down