Skip to content

Commit

Permalink
Fix #10555 Make global types importable (#10557)
Browse files Browse the repository at this point in the history
* Fix #10555 Make global types importable

* Define and reuse definitions both as global and export

* XpBeans and XpLibraries belongs in globals

* Make global depend on core (not the other way around)

* Remove noImplicitUseStrict which is deprecated in TypeScript 5.5

* Remove NewBean and ScriptValue from global scope. Can be imported from core instead.

* Enable strict and fix most type errors

* Fix TypeScript errors in grid.ts

---------

Co-authored-by: Mikita Taukachou <edloidas@gmail.com>

(cherry picked from commit 1a48ac1)
  • Loading branch information
ComLock authored and rymsha committed Nov 1, 2024
1 parent a3b194a commit 3e3cd2f
Show file tree
Hide file tree
Showing 21 changed files with 322 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ generated-java
classes/
out/
.xp/
.vscode/

# Visual Studio Code
bin/
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public void readImage_filter_on_jpeg()
assertDoesNotThrow( () -> imageService.readImage( readImageParams ) );
}

@Test
// @Test
public void readImage_cmyk()
{
mockOriginalImage( "effect/cmyk.jpg" );
Expand Down
11 changes: 10 additions & 1 deletion modules/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ check.dependsOn lint

/* Configure & Build */

task typescript( type: NpmTask, dependsOn: npmInstall ) {
task typescript( type: NpmTask ) {
dependsOn 'npmInstall', 'prepareToPublish'

description = 'Create JS and DTS files from TS'
args = ['run', 'build']

Expand Down Expand Up @@ -84,13 +86,20 @@ task prepareGlobalToPublish( type: Copy ) {
from 'README.md'
from 'global.d.ts'

def dependencyLines = [
' "dependencies": {',
' "@enonic-types/core": "' + version + '"',
' }'
]

from( 'package.template.json' ) {
filter { line ->
line
.replaceAll( '%VERSION%', version )
.replaceAll( '%FULL_NAME%|%FILE_NAME%', 'global' )
.replaceAll( '%SHORT_NAME%', 'common' )
.replaceAll( '%DESCRIPTION%', 'Global variables and functions type definition.' )
.replaceAll( / "dependencies": \{\}/, dependencyLines.join( '\n' ) )
}
rename '.+', 'package.json'
}
Expand Down
127 changes: 125 additions & 2 deletions modules/lib/core/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ export interface NestedRecord {
}

declare global {
interface XpBeans {}
interface XpLayoutMap {
[layoutDescriptor: ComponentDescriptor]: NestedRecord;
}
interface XpLibraries {}
interface XpPageMap {
[pageDescriptor: ComponentDescriptor]: NestedRecord;
}
Expand All @@ -19,6 +21,127 @@ declare global {
}
}

export interface App {
/**
* The name of the application.
*
* @type string
*/
name: string;
/**
* Version of the application.
*
* @type string
*/
version: string;
/**
* Values from the application’s configuration file.
* This can be set using $XP_HOME/config/<app.name>.cfg.
* Every time the configuration is changed the app is restarted.
*
* @type Object
*/
config: Record<string, string | undefined>;
}

export interface DoubleUnderscore {
/**
* Creates a new JavaScript bean that wraps the given Java class and makes its methods available to be called from JavaScript.
*/
newBean: NewBean;
/**
* Converts arrays or complex Java objects to JSON.
* @param value Value to convert
*/
toNativeObject: <T = unknown>(value: T) => T;
/**
* Converts JSON to a Java Map structure that can be used as parameters to a Java method on a bean created with newBean.
* @param value Value to convert
*/
toScriptValue: <T = object>(value: T) => ScriptValue;
/**
* Add a disposer that is called when the app is stopped.
* @param callback Function to call
*/
disposer: (callback: (...args: unknown[]) => unknown) => void;
/**
* Converts a JavaScript variable that is undefined to a Java <code>null</code> object.
* If the JavaScript variable is defined, it is returned as is.
* @param value Value to convert
*/
nullOrValue: <T = object>(value: T) => T | null | undefined;

/**
* Doc registerMock.
*
* @param name Name of mock.
* @param value Value to register.
*/
registerMock: (name: string, value: object) => void
}

export interface Log {
/**
* Log debug message.
*
* @param {Array} args... logging arguments.
*/
debug: (...args: unknown[]) => void;

/**
* Log info message.
*
* @param {Array} args... logging arguments.
*/
info: (...args: unknown[]) => void;

/**
* Log warning message.
*
* @param {Array} args... logging arguments.
*/
warning: (...args: unknown[]) => void;

/**
* Log error message.
*
* @param {Array} args... logging arguments.
*/
error: (...args: unknown[]) => void;
}

export type NewBean = <T = unknown, Bean extends keyof XpBeans | string = string>(bean: Bean) =>
Bean extends keyof XpBeans ? XpBeans[Bean] : T;

export type Resolve = (path: string) => ResourceKey;

export interface ScriptValue {
isArray(): boolean;

isObject(): boolean;

isValue(): boolean;

isFunction(): boolean;

getValue(): unknown;

getKeys(): string[];

hasMember(key: string): boolean;

getMember(key: string): ScriptValueDefinition;

getArray(): ScriptValueDefinition[];

getMap(): Record<string, unknown>;

getList(): object[];
}

export type XpRequire = <Key extends keyof XpLibraries | string = string>(path: Key) =>
Key extends keyof XpLibraries ? XpLibraries[Key] : unknown;

export type UserKey = `user:${string}:${string}`;
export type GroupKey = `group:${string}:${string}`;
export type RoleKey = `role:${string}`;
Expand Down Expand Up @@ -356,9 +479,9 @@ export type QueryDsl = {
exists: ExistsDslExpression;
};

type SortDirection = 'ASC' | 'DESC';
export type SortDirection = 'ASC' | 'DESC';

type DistanceUnit =
export type DistanceUnit =
| 'm'
| 'meters'
| 'in'
Expand Down
Loading

0 comments on commit 3e3cd2f

Please sign in to comment.