@tstdl/base 0.84.1 → 0.84.3
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/browser/browser-controller.d.ts +2 -8
- package/browser/browser-controller.js +1 -16
- package/browser/browser.service.d.ts +2 -2
- package/browser/browser.service.js +3 -3
- package/browser/element-controller.d.ts +37 -0
- package/browser/element-controller.js +91 -0
- package/browser/index.d.ts +2 -0
- package/browser/index.js +2 -0
- package/browser/page-controller.d.ts +22 -37
- package/browser/page-controller.js +54 -65
- package/browser/types.d.ts +10 -0
- package/browser/types.js +16 -0
- package/browser/utils.d.ts +3 -1
- package/browser/utils.js +11 -0
- package/core.d.ts +3 -0
- package/core.js +13 -1
- package/examples/browser/basic.js +4 -3
- package/package.json +5 -5
- package/pdf/pdf.service.js +9 -2
- package/schema/coercers/uint8-array.coercer.js +1 -1
- package/schema/schema.d.ts +2 -1
- package/schema/schema.error.js +3 -2
- package/schema/schema.js +18 -18
- package/schema/utils/value-type.js +3 -2
|
@@ -3,10 +3,9 @@ import type { Injectable } from '../container/interfaces.js';
|
|
|
3
3
|
import { resolveArgumentType } from '../container/interfaces.js';
|
|
4
4
|
import type { AsyncDisposable } from '../disposable/disposable.js';
|
|
5
5
|
import { disposeAsync } from '../disposable/disposable.js';
|
|
6
|
-
import type {
|
|
6
|
+
import type { BrowserContextState, NewPageOptions } from './browser-context-controller.js';
|
|
7
7
|
import { BrowserContextController } from './browser-context-controller.js';
|
|
8
8
|
import type { NewBrowserOptions } from './browser.service.js';
|
|
9
|
-
import type { PageController } from './page-controller.js';
|
|
10
9
|
export type BrowserControllerOptions = {
|
|
11
10
|
defaultNewContextOptions?: NewBrowserContextOptions;
|
|
12
11
|
};
|
|
@@ -14,7 +13,7 @@ export type NewBrowserContextOptions = {
|
|
|
14
13
|
state?: BrowserContextState;
|
|
15
14
|
locale?: string;
|
|
16
15
|
extraHttpHeaders?: Record<string, string>;
|
|
17
|
-
|
|
16
|
+
defaultNewPageOptions?: NewPageOptions;
|
|
18
17
|
viewport?: {
|
|
19
18
|
width: number;
|
|
20
19
|
height: number;
|
|
@@ -35,11 +34,6 @@ export declare class BrowserController implements AsyncDisposable, Injectable<Br
|
|
|
35
34
|
constructor(browser: Browser, options?: BrowserControllerOptions);
|
|
36
35
|
[disposeAsync](): Promise<void>;
|
|
37
36
|
newContext(options?: NewBrowserContextOptions): Promise<BrowserContextController>;
|
|
38
|
-
/**
|
|
39
|
-
* Creates a new context and a new page. Context is automatically closed on page close.
|
|
40
|
-
* Only use for isolated or "use once" scenarios, otherwise all the contexts are more expensive than using a single context.
|
|
41
|
-
*/
|
|
42
|
-
newPage(options?: NewBrowserContextOptions & NewPageOptions): Promise<PageController>;
|
|
43
37
|
close(): Promise<void>;
|
|
44
38
|
waitForClose(): Promise<void>;
|
|
45
39
|
}
|
|
@@ -61,22 +61,7 @@ let BrowserController = class BrowserController2 {
|
|
|
61
61
|
proxy: mergedOptions.proxy,
|
|
62
62
|
extraHTTPHeaders: mergedOptions.extraHttpHeaders
|
|
63
63
|
});
|
|
64
|
-
return new import_browser_context_controller.BrowserContextController(context, mergedOptions
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Creates a new context and a new page. Context is automatically closed on page close.
|
|
68
|
-
* Only use for isolated or "use once" scenarios, otherwise all the contexts are more expensive than using a single context.
|
|
69
|
-
*/
|
|
70
|
-
async newPage(options) {
|
|
71
|
-
const context = await this.newContext(options);
|
|
72
|
-
try {
|
|
73
|
-
const page = await context.newPage(options);
|
|
74
|
-
page.ownedContext = context;
|
|
75
|
-
return page;
|
|
76
|
-
} catch (error) {
|
|
77
|
-
await context.close();
|
|
78
|
-
throw error;
|
|
79
|
-
}
|
|
64
|
+
return new import_browser_context_controller.BrowserContextController(context, mergedOptions);
|
|
80
65
|
}
|
|
81
66
|
async close() {
|
|
82
67
|
if (this.browser.isConnected()) {
|
|
@@ -3,7 +3,7 @@ import { afterResolve, resolveArgumentType } from '../container/interfaces.js';
|
|
|
3
3
|
import type { AsyncDisposable } from '../disposable/disposable.js';
|
|
4
4
|
import { disposeAsync } from '../disposable/disposable.js';
|
|
5
5
|
import { BrowserContextController } from './browser-context-controller.js';
|
|
6
|
-
import type {
|
|
6
|
+
import type { NewBrowserContextOptions } from './browser-controller.js';
|
|
7
7
|
import { BrowserController } from './browser-controller.js';
|
|
8
8
|
export declare class BrowserServiceOptions {
|
|
9
9
|
defaultNewBrowserOptions?: NewBrowserOptions;
|
|
@@ -18,7 +18,7 @@ export type NewBrowserOptions = {
|
|
|
18
18
|
};
|
|
19
19
|
/** @deprecated should be avoided */
|
|
20
20
|
browserArguments?: string[];
|
|
21
|
-
|
|
21
|
+
defaultNewContextOptions?: NewBrowserContextOptions;
|
|
22
22
|
};
|
|
23
23
|
export declare class BrowserService implements AsyncDisposable, Injectable<BrowserServiceArgument> {
|
|
24
24
|
private readonly browsers;
|
|
@@ -72,11 +72,11 @@ let BrowserService = class BrowserService2 {
|
|
|
72
72
|
const browser = await (0, import_utils.getBrowserType)(mergedOptions.browser).launch(launchOptions);
|
|
73
73
|
this.browsers.add(browser);
|
|
74
74
|
browser.once("disconnected", () => this.browsers.delete(browser));
|
|
75
|
-
return new import_browser_controller.BrowserController(browser, mergedOptions.
|
|
75
|
+
return new import_browser_controller.BrowserController(browser, { defaultNewContextOptions: mergedOptions.defaultNewContextOptions });
|
|
76
76
|
}
|
|
77
77
|
async newPersistentContext(dataDirectory, browserOptions = {}, contextOptions = {}) {
|
|
78
78
|
const mergedBrowserOptions = { ...this.options?.defaultNewBrowserOptions, ...browserOptions };
|
|
79
|
-
const mergedContextOptions = (0, import_utils.mergeNewBrowserContextOptions)(this.options?.defaultNewBrowserOptions?.
|
|
79
|
+
const mergedContextOptions = (0, import_utils.mergeNewBrowserContextOptions)(this.options?.defaultNewBrowserOptions?.defaultNewContextOptions, browserOptions.defaultNewContextOptions, contextOptions);
|
|
80
80
|
const launchOptions = (0, import_utils.getLaunchOptions)(mergedBrowserOptions);
|
|
81
81
|
const context = await (0, import_utils.getBrowserType)(mergedBrowserOptions.browser).launchPersistentContext(dataDirectory, {
|
|
82
82
|
...launchOptions,
|
|
@@ -87,7 +87,7 @@ let BrowserService = class BrowserService2 {
|
|
|
87
87
|
});
|
|
88
88
|
this.persistentBrowserContexts.add(context);
|
|
89
89
|
context.once("close", () => this.persistentBrowserContexts.delete(context));
|
|
90
|
-
return new import_browser_context_controller.BrowserContextController(context, mergedBrowserOptions.
|
|
90
|
+
return new import_browser_context_controller.BrowserContextController(context, mergedBrowserOptions.defaultNewContextOptions);
|
|
91
91
|
}
|
|
92
92
|
async dispose() {
|
|
93
93
|
for (const browser of this.browsers) {
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Merge, NonUndefinable, TypedOmit } from '../types.js';
|
|
2
|
+
import type { ValueOrProvider } from '../utils/value-or-provider.js';
|
|
3
|
+
import type { ElementHandle, Locator } from 'playwright';
|
|
4
|
+
import { PageController } from './page-controller.js';
|
|
5
|
+
export type Delay = ValueOrProvider<number>;
|
|
6
|
+
export type ActionDelayOptions = {
|
|
7
|
+
actionDelay?: Delay;
|
|
8
|
+
};
|
|
9
|
+
export type TypeDelayOptions = {
|
|
10
|
+
typeDelay?: Delay;
|
|
11
|
+
};
|
|
12
|
+
export type ClickDelayOptions = {
|
|
13
|
+
clickDelay?: Delay;
|
|
14
|
+
};
|
|
15
|
+
export type ElementControllerOptions = {
|
|
16
|
+
actionDelay?: Delay;
|
|
17
|
+
typeDelay?: Delay;
|
|
18
|
+
};
|
|
19
|
+
type LocatorOptions<K extends keyof Locator, I extends keyof Parameters<Locator[K]>> = NonUndefinable<Parameters<Locator[K]>[I]>;
|
|
20
|
+
export declare class ElementController {
|
|
21
|
+
readonly locatorOrHandle: Locator | ElementHandle;
|
|
22
|
+
readonly pageController: PageController;
|
|
23
|
+
readonly options: ElementControllerOptions;
|
|
24
|
+
constructor(locatorOrHandle: Locator | ElementHandle, pageController: PageController, options?: ElementControllerOptions);
|
|
25
|
+
waitFor(options?: Parameters<Locator['waitFor']>[0]): Promise<void>;
|
|
26
|
+
fill(text: string, options?: Merge<LocatorOptions<'fill', 1>, ActionDelayOptions>): Promise<void>;
|
|
27
|
+
type(text: string, options?: Merge<TypedOmit<LocatorOptions<'type', 1>, 'delay'>, ActionDelayOptions & TypeDelayOptions>): Promise<void>;
|
|
28
|
+
selectOption(value: string | string[], options?: Merge<LocatorOptions<'selectOption', 1>, ActionDelayOptions>): Promise<void>;
|
|
29
|
+
click(options?: Merge<TypedOmit<LocatorOptions<'click', 0>, 'delay'>, ActionDelayOptions & ClickDelayOptions>): Promise<void>;
|
|
30
|
+
getValue(options?: LocatorOptions<'inputValue', 0>): Promise<string>;
|
|
31
|
+
/**
|
|
32
|
+
* Get PageController for element. Element must be a frame.
|
|
33
|
+
*/
|
|
34
|
+
getPage(options?: LocatorOptions<'elementHandle', 0>): Promise<PageController>;
|
|
35
|
+
private prepareAction;
|
|
36
|
+
}
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var element_controller_exports = {};
|
|
20
|
+
__export(element_controller_exports, {
|
|
21
|
+
ElementController: () => ElementController
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(element_controller_exports);
|
|
24
|
+
var import_timing = require("../utils/timing.js");
|
|
25
|
+
var import_type_guards = require("../utils/type-guards.js");
|
|
26
|
+
var import_value_or_provider = require("../utils/value-or-provider.js");
|
|
27
|
+
var import_page_controller = require("./page-controller.js");
|
|
28
|
+
var import_utils = require("./utils.js");
|
|
29
|
+
class ElementController {
|
|
30
|
+
locatorOrHandle;
|
|
31
|
+
pageController;
|
|
32
|
+
options;
|
|
33
|
+
constructor(locatorOrHandle, pageController, options = {}) {
|
|
34
|
+
this.locatorOrHandle = locatorOrHandle;
|
|
35
|
+
this.pageController = pageController;
|
|
36
|
+
this.options = options;
|
|
37
|
+
}
|
|
38
|
+
async waitFor(options) {
|
|
39
|
+
return this.locatorOrHandle.waitFor(options);
|
|
40
|
+
}
|
|
41
|
+
async fill(text, options) {
|
|
42
|
+
await this.prepareAction(options);
|
|
43
|
+
await this.locatorOrHandle.fill(text, options);
|
|
44
|
+
}
|
|
45
|
+
async type(text, options) {
|
|
46
|
+
await this.prepareAction(options);
|
|
47
|
+
if ((0, import_type_guards.isUndefined)(this.options.typeDelay)) {
|
|
48
|
+
await this.locatorOrHandle.type(text, options);
|
|
49
|
+
} else {
|
|
50
|
+
for (const char of text) {
|
|
51
|
+
await this.locatorOrHandle.type(char, options);
|
|
52
|
+
await delay(options?.typeDelay ?? this.options.typeDelay);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async selectOption(value, options) {
|
|
57
|
+
await this.prepareAction(options);
|
|
58
|
+
await this.locatorOrHandle.selectOption(value, options);
|
|
59
|
+
}
|
|
60
|
+
async click(options) {
|
|
61
|
+
await this.prepareAction(options);
|
|
62
|
+
await this.locatorOrHandle.click({
|
|
63
|
+
...options,
|
|
64
|
+
delay: (0, import_value_or_provider.resolveValueOrProvider)(options?.clickDelay)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
async getValue(options) {
|
|
68
|
+
return this.locatorOrHandle.inputValue(options);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get PageController for element. Element must be a frame.
|
|
72
|
+
*/
|
|
73
|
+
async getPage(options) {
|
|
74
|
+
const handle = (0, import_utils.isLocator)(this.locatorOrHandle) ? await this.locatorOrHandle.elementHandle(options) : this.locatorOrHandle;
|
|
75
|
+
if ((0, import_type_guards.isNull)(handle)) {
|
|
76
|
+
throw new Error("Could not get element handle.");
|
|
77
|
+
}
|
|
78
|
+
const frame = await this.locatorOrHandle.contentFrame();
|
|
79
|
+
(0, import_type_guards.assertNotNull)(frame, "Could not get content frame for element handle.");
|
|
80
|
+
return new import_page_controller.PageController(frame.page(), this.pageController.options);
|
|
81
|
+
}
|
|
82
|
+
async prepareAction(options) {
|
|
83
|
+
await delay(options?.actionDelay ?? this.options.actionDelay);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function delay(milliseconds) {
|
|
87
|
+
if ((0, import_type_guards.isUndefined)(milliseconds)) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
await (0, import_timing.timeout)((0, import_value_or_provider.resolveValueOrProvider)(milliseconds));
|
|
91
|
+
}
|
package/browser/index.d.ts
CHANGED
package/browser/index.js
CHANGED
|
@@ -18,4 +18,6 @@ module.exports = __toCommonJS(browser_exports);
|
|
|
18
18
|
__reExport(browser_exports, require("./browser-context-controller.js"), module.exports);
|
|
19
19
|
__reExport(browser_exports, require("./browser-controller.js"), module.exports);
|
|
20
20
|
__reExport(browser_exports, require("./browser.service.js"), module.exports);
|
|
21
|
+
__reExport(browser_exports, require("./element-controller.js"), module.exports);
|
|
21
22
|
__reExport(browser_exports, require("./page-controller.js"), module.exports);
|
|
23
|
+
__reExport(browser_exports, require("./types.js"), module.exports);
|
|
@@ -1,54 +1,39 @@
|
|
|
1
|
-
import type { Page } from 'playwright';
|
|
1
|
+
import type { ElementHandle, Page } from 'playwright';
|
|
2
2
|
import type { AsyncDisposable } from '../disposable/disposable.js';
|
|
3
3
|
import { disposeAsync } from '../disposable/disposable.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { NonUndefinable, SimplifyObject } from '../types.js';
|
|
5
|
+
import type { Delay } from './element-controller.js';
|
|
6
|
+
import { ElementController } from './element-controller.js';
|
|
5
7
|
import type { PdfRenderOptions } from './pdf-options.js';
|
|
6
|
-
|
|
7
|
-
export type DelayProvider = () => number;
|
|
8
|
+
import type { Abortable } from './types.js';
|
|
8
9
|
export type PageControllerOptions = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
export type WaitOptions = {
|
|
13
|
-
timeout?: number;
|
|
14
|
-
};
|
|
15
|
-
export type LoadState = 'load' | 'domcontentloaded' | 'networkidle';
|
|
16
|
-
export type WaitForStateOptions = {
|
|
17
|
-
waitUntil: LoadState;
|
|
18
|
-
};
|
|
19
|
-
export type DelayOptions = {
|
|
20
|
-
delay?: Delay;
|
|
21
|
-
};
|
|
22
|
-
export type Abortable = {
|
|
23
|
-
abort?: AbortSignal;
|
|
24
|
-
};
|
|
25
|
-
/** @deprecated for internal use only */
|
|
26
|
-
export type PageControllerInternal = {
|
|
27
|
-
ownedContext?: BrowserContextController;
|
|
10
|
+
defaultActionDelay?: Delay;
|
|
11
|
+
defaultTypeDelay?: Delay;
|
|
28
12
|
};
|
|
29
13
|
export declare class PageController implements AsyncDisposable {
|
|
30
|
-
private readonly
|
|
14
|
+
private readonly elementControllerOptions;
|
|
31
15
|
/** @deprecated should be avoided */
|
|
32
16
|
readonly page: Page;
|
|
33
17
|
readonly options: PageControllerOptions;
|
|
34
18
|
constructor(page: Page, options?: PageControllerOptions);
|
|
35
19
|
[disposeAsync](): Promise<void>;
|
|
36
20
|
close(): Promise<void>;
|
|
37
|
-
setContent(
|
|
21
|
+
setContent(...args: Parameters<Page['setContent']>): Promise<void>;
|
|
38
22
|
setExtraHttpHeaders(headers: Record<string, string>): Promise<void>;
|
|
39
|
-
navigate(
|
|
23
|
+
navigate(...args: Parameters<Page['goto']>): Promise<void>;
|
|
40
24
|
waitForClose(): Promise<void>;
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
waitForFrame(selector: string, options
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
25
|
+
waitForLoadState(...args: Parameters<Page['waitForLoadState']>): Promise<void>;
|
|
26
|
+
waitForUrl(...args: Parameters<Page['waitForURL']>): Promise<void>;
|
|
27
|
+
waitForElement(selector: string, options: Parameters<Page['waitForSelector']>[1]): Promise<ElementHandle>;
|
|
28
|
+
waitForFrame(selector: string, options: Parameters<Page['waitForSelector']>[1]): Promise<PageController>;
|
|
29
|
+
getFrame(selector: Parameters<Page['frame']>[0]): PageController;
|
|
30
|
+
getBySelector(selector: string, options?: SimplifyObject<Pick<NonUndefinable<Parameters<Page['locator']>[1]>, 'hasText' | 'hasNotText'>>): ElementController;
|
|
31
|
+
getByRole(role: Parameters<Page['getByRole']>[0], options?: Parameters<Page['getByRole']>[1]): ElementController;
|
|
32
|
+
getByLabel(text: Parameters<Page['getByLabel']>[0], options?: Parameters<Page['getByLabel']>[1]): ElementController;
|
|
33
|
+
getByAltText(text: Parameters<Page['getByAltText']>[0], options?: Parameters<Page['getByAltText']>[1]): ElementController;
|
|
34
|
+
getByPlaceholder(text: Parameters<Page['getByPlaceholder']>[0], options?: Parameters<Page['getByPlaceholder']>[1]): ElementController;
|
|
35
|
+
getByText(text: Parameters<Page['getByText']>[0], options?: Parameters<Page['getByText']>[1]): ElementController;
|
|
36
|
+
getByTitle(text: Parameters<Page['getByTitle']>[0], options?: Parameters<Page['getByTitle']>[1]): ElementController;
|
|
51
37
|
renderPdf(options?: PdfRenderOptions & Abortable): Promise<Uint8Array>;
|
|
52
38
|
renderPdfStream(options?: PdfRenderOptions & Abortable): ReadableStream<Uint8Array>;
|
|
53
|
-
private prepareAction;
|
|
54
39
|
}
|
|
@@ -26,33 +26,31 @@ var import_readable_stream_from_promise = require("../utils/stream/readable-stre
|
|
|
26
26
|
var import_timing = require("../utils/timing.js");
|
|
27
27
|
var import_type_guards = require("../utils/type-guards.js");
|
|
28
28
|
var import_units = require("../utils/units.js");
|
|
29
|
+
var import_element_controller = require("./element-controller.js");
|
|
29
30
|
class PageController {
|
|
30
|
-
|
|
31
|
+
elementControllerOptions;
|
|
31
32
|
/** @deprecated should be avoided */
|
|
32
33
|
page;
|
|
33
34
|
options;
|
|
34
35
|
constructor(page, options = {}) {
|
|
35
36
|
this.page = page;
|
|
36
37
|
this.options = options;
|
|
38
|
+
this.elementControllerOptions = { actionDelay: this.options.defaultActionDelay, typeDelay: this.options.defaultTypeDelay };
|
|
37
39
|
}
|
|
38
40
|
async [import_disposable.disposeAsync]() {
|
|
39
41
|
await this.close();
|
|
40
42
|
}
|
|
41
43
|
async close() {
|
|
42
|
-
|
|
43
|
-
await this.ownedContext.close();
|
|
44
|
-
} else {
|
|
45
|
-
await this.page.close();
|
|
46
|
-
}
|
|
44
|
+
await this.page.close();
|
|
47
45
|
}
|
|
48
|
-
async setContent(
|
|
49
|
-
await this.page.setContent(
|
|
46
|
+
async setContent(...args) {
|
|
47
|
+
await this.page.setContent(...args);
|
|
50
48
|
}
|
|
51
49
|
async setExtraHttpHeaders(headers) {
|
|
52
50
|
await this.page.setExtraHTTPHeaders(headers);
|
|
53
51
|
}
|
|
54
|
-
async navigate(
|
|
55
|
-
await this.page.goto(
|
|
52
|
+
async navigate(...args) {
|
|
53
|
+
await this.page.goto(...args);
|
|
56
54
|
}
|
|
57
55
|
async waitForClose() {
|
|
58
56
|
return new Promise((resolve) => {
|
|
@@ -63,56 +61,61 @@ class PageController {
|
|
|
63
61
|
this.page.once("close", () => resolve());
|
|
64
62
|
});
|
|
65
63
|
}
|
|
66
|
-
async
|
|
67
|
-
await this.page.waitForLoadState(
|
|
64
|
+
async waitForLoadState(...args) {
|
|
65
|
+
await this.page.waitForLoadState(...args);
|
|
68
66
|
}
|
|
69
|
-
async
|
|
70
|
-
await this.page.
|
|
67
|
+
async waitForUrl(...args) {
|
|
68
|
+
await this.page.waitForURL(...args);
|
|
71
69
|
}
|
|
72
|
-
async
|
|
73
|
-
await this.page.
|
|
70
|
+
async waitForElement(selector, options) {
|
|
71
|
+
const element = await this.page.waitForSelector(selector, options);
|
|
72
|
+
if ((0, import_type_guards.isNull)(element)) {
|
|
73
|
+
throw new Error("Element not found.");
|
|
74
|
+
}
|
|
75
|
+
return element;
|
|
74
76
|
}
|
|
75
77
|
async waitForFrame(selector, options) {
|
|
76
|
-
const
|
|
77
|
-
const frame = await
|
|
78
|
-
(0, import_type_guards.
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
async fill(selector, text, options) {
|
|
82
|
-
await this.prepareAction();
|
|
83
|
-
const locator = this.page.locator(selector);
|
|
84
|
-
await locator.fill(text, { timeout: options?.timeout });
|
|
85
|
-
}
|
|
86
|
-
async type(selector, text, options) {
|
|
87
|
-
await this.prepareAction();
|
|
88
|
-
const locator = this.page.locator(selector);
|
|
89
|
-
await locator.focus({ timeout: options?.timeout });
|
|
90
|
-
if ((0, import_type_guards.isUndefined)(this.options.typeDelay)) {
|
|
91
|
-
await locator.type(text, { timeout: options?.timeout });
|
|
92
|
-
} else {
|
|
93
|
-
for (const char of text) {
|
|
94
|
-
await locator.type(char, { timeout: options?.timeout });
|
|
95
|
-
await delay(options?.delay ?? this.options.typeDelay);
|
|
96
|
-
}
|
|
78
|
+
const element = await this.waitForElement(selector, options);
|
|
79
|
+
const frame = await element.contentFrame();
|
|
80
|
+
if ((0, import_type_guards.isNull)(frame)) {
|
|
81
|
+
throw new Error("Element is not a frame.");
|
|
97
82
|
}
|
|
83
|
+
return new PageController(frame.page(), this.options);
|
|
98
84
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
85
|
+
getFrame(selector) {
|
|
86
|
+
const frame = this.page.frame(selector);
|
|
87
|
+
if ((0, import_type_guards.isNull)(frame)) {
|
|
88
|
+
throw new Error("Frame not found.");
|
|
89
|
+
}
|
|
90
|
+
return new PageController(frame.page(), this.options);
|
|
103
91
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
await locator.click({ delay: 50, timeout: options?.timeout });
|
|
92
|
+
getBySelector(selector, options) {
|
|
93
|
+
const locator = this.page.locator(selector, options);
|
|
94
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
108
95
|
}
|
|
109
|
-
|
|
110
|
-
const locator = this.page.
|
|
111
|
-
return
|
|
96
|
+
getByRole(role, options) {
|
|
97
|
+
const locator = this.page.getByRole(role, options);
|
|
98
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
112
99
|
}
|
|
113
|
-
|
|
114
|
-
const locator = this.page.
|
|
115
|
-
return
|
|
100
|
+
getByLabel(text, options) {
|
|
101
|
+
const locator = this.page.getByLabel(text, options);
|
|
102
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
103
|
+
}
|
|
104
|
+
getByAltText(text, options) {
|
|
105
|
+
const locator = this.page.getByAltText(text, options);
|
|
106
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
107
|
+
}
|
|
108
|
+
getByPlaceholder(text, options) {
|
|
109
|
+
const locator = this.page.getByPlaceholder(text, options);
|
|
110
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
111
|
+
}
|
|
112
|
+
getByText(text, options) {
|
|
113
|
+
const locator = this.page.getByText(text, options);
|
|
114
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
115
|
+
}
|
|
116
|
+
getByTitle(text, options) {
|
|
117
|
+
const locator = this.page.getByTitle(text, options);
|
|
118
|
+
return new import_element_controller.ElementController(locator, this, this.elementControllerOptions);
|
|
116
119
|
}
|
|
117
120
|
async renderPdf(options = {}) {
|
|
118
121
|
const createPdfOptions = convertPdfOptions(options);
|
|
@@ -129,20 +132,6 @@ class PageController {
|
|
|
129
132
|
});
|
|
130
133
|
});
|
|
131
134
|
}
|
|
132
|
-
async prepareAction() {
|
|
133
|
-
await delay(this.options.actionDelay);
|
|
134
|
-
await this.waitForNetworkIdle();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
async function delay(milliseconds) {
|
|
138
|
-
if ((0, import_type_guards.isUndefined)(milliseconds)) {
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
if ((0, import_type_guards.isNumber)(milliseconds)) {
|
|
142
|
-
await (0, import_timing.timeout)(milliseconds);
|
|
143
|
-
} else {
|
|
144
|
-
await (0, import_timing.timeout)(milliseconds());
|
|
145
|
-
}
|
|
146
135
|
}
|
|
147
136
|
function convertPdfOptions(options) {
|
|
148
137
|
const margin = (0, import_type_guards.isUndefined)(options.margin) ? void 0 : (0, import_type_guards.isObject)(options.margin) ? options.margin : {
|
package/browser/types.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var types_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(types_exports);
|
package/browser/utils.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import type { BrowserType, LaunchOptions } from 'playwright';
|
|
1
|
+
import type { BrowserType, ElementHandle, LaunchOptions, Locator } from 'playwright';
|
|
2
2
|
import type { NewBrowserContextOptions } from './browser-controller.js';
|
|
3
3
|
import type { NewBrowserOptions } from './browser.service.js';
|
|
4
4
|
export declare function getLaunchOptions(options: NewBrowserOptions): LaunchOptions;
|
|
5
5
|
export declare function mergeNewBrowserContextOptions(a: NewBrowserContextOptions | undefined, b?: NewBrowserContextOptions, c?: NewBrowserContextOptions): NewBrowserContextOptions;
|
|
6
6
|
export declare function getBrowserType(type: string | undefined): BrowserType;
|
|
7
|
+
export declare function isLocator(value: Locator | ElementHandle): value is Locator;
|
|
8
|
+
export declare function isElementHandle(value: Locator | ElementHandle): value is ElementHandle;
|
package/browser/utils.js
CHANGED
|
@@ -20,11 +20,14 @@ var utils_exports = {};
|
|
|
20
20
|
__export(utils_exports, {
|
|
21
21
|
getBrowserType: () => getBrowserType,
|
|
22
22
|
getLaunchOptions: () => getLaunchOptions,
|
|
23
|
+
isElementHandle: () => isElementHandle,
|
|
24
|
+
isLocator: () => isLocator,
|
|
23
25
|
mergeNewBrowserContextOptions: () => mergeNewBrowserContextOptions
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(utils_exports);
|
|
26
28
|
var import_not_supported_error = require("../error/not-supported.error.js");
|
|
27
29
|
var import_object = require("../utils/object/object.js");
|
|
30
|
+
var import_type_guards = require("../utils/type-guards.js");
|
|
28
31
|
var import_playwright = require("playwright");
|
|
29
32
|
function getLaunchOptions(options) {
|
|
30
33
|
const { windowSize, browserArguments, headless } = options;
|
|
@@ -58,3 +61,11 @@ function getBrowserType(type) {
|
|
|
58
61
|
}
|
|
59
62
|
return browserType;
|
|
60
63
|
}
|
|
64
|
+
const exclusiveLocatorKey = "locator";
|
|
65
|
+
function isLocator(value) {
|
|
66
|
+
return (0, import_type_guards.isFunction)(value[exclusiveLocatorKey]);
|
|
67
|
+
}
|
|
68
|
+
const exclusiveElementHandleKey = "$";
|
|
69
|
+
function isElementHandle(value) {
|
|
70
|
+
return (0, import_type_guards.isFunction)(value[exclusiveElementHandleKey]);
|
|
71
|
+
}
|
package/core.d.ts
CHANGED
|
@@ -4,9 +4,12 @@ import type { LoggerArgument } from './logger/index.js';
|
|
|
4
4
|
import { LogLevel, Logger } from './logger/index.js';
|
|
5
5
|
export declare const CORE_LOGGER: InjectionToken<Logger, any>;
|
|
6
6
|
export declare const disposer: AsyncDisposer;
|
|
7
|
+
export declare function isDevMode(): boolean;
|
|
8
|
+
export declare function enableProductionMode(): void;
|
|
7
9
|
export declare function connect(name: string, connectFunction: (() => Promise<any>), logger: Logger, maxTries?: number): Promise<void>;
|
|
8
10
|
export declare function disposeInstances(): Promise<void>;
|
|
9
11
|
export type CoreConfiguration = {
|
|
12
|
+
production?: boolean;
|
|
10
13
|
logger?: InjectionToken<Logger, LoggerArgument>;
|
|
11
14
|
logLevel?: LogLevel;
|
|
12
15
|
coreLogPrefix?: string;
|
package/core.js
CHANGED
|
@@ -22,7 +22,9 @@ __export(core_exports, {
|
|
|
22
22
|
configureTstdl: () => configureTstdl,
|
|
23
23
|
connect: () => connect,
|
|
24
24
|
disposeInstances: () => disposeInstances,
|
|
25
|
-
disposer: () => disposer
|
|
25
|
+
disposer: () => disposer,
|
|
26
|
+
enableProductionMode: () => enableProductionMode,
|
|
27
|
+
isDevMode: () => isDevMode
|
|
26
28
|
});
|
|
27
29
|
module.exports = __toCommonJS(core_exports);
|
|
28
30
|
var import_container = require("./container/index.js");
|
|
@@ -33,6 +35,13 @@ var import_timing = require("./utils/timing.js");
|
|
|
33
35
|
var import_type_guards = require("./utils/type-guards.js");
|
|
34
36
|
const CORE_LOGGER = (0, import_container.injectionToken)("core logger");
|
|
35
37
|
const disposer = new import_async_disposer.AsyncDisposer();
|
|
38
|
+
let _isDevMode = true;
|
|
39
|
+
function isDevMode() {
|
|
40
|
+
return _isDevMode;
|
|
41
|
+
}
|
|
42
|
+
function enableProductionMode() {
|
|
43
|
+
_isDevMode = false;
|
|
44
|
+
}
|
|
36
45
|
async function connect(name, connectFunction, logger, maxTries = 5) {
|
|
37
46
|
let triesLeft = maxTries;
|
|
38
47
|
let success = false;
|
|
@@ -57,6 +66,9 @@ async function disposeInstances() {
|
|
|
57
66
|
}
|
|
58
67
|
let coreLogPrefix;
|
|
59
68
|
function configureTstdl(config = {}) {
|
|
69
|
+
if (config.production == true) {
|
|
70
|
+
enableProductionMode();
|
|
71
|
+
}
|
|
60
72
|
import_container.container.register(import_logger2.Logger, { useToken: config.logger ?? import_logger.ConsoleLogger });
|
|
61
73
|
import_container.container.registerSingleton(import_logger2.LogLevel, { useFactory: (level) => (0, import_type_guards.assertDefinedPass)(level, "LogLevel argument not provided") }, { defaultArgumentProvider: () => config.logLevel ?? import_logger2.LogLevel.Trace });
|
|
62
74
|
if ((0, import_type_guards.isDefined)(config.coreLogPrefix)) {
|
|
@@ -7,13 +7,14 @@ var import_timing = require("../../utils/timing.js");
|
|
|
7
7
|
async function main() {
|
|
8
8
|
const browserService = await import_container.container.resolveAsync(import_browser_service.BrowserService);
|
|
9
9
|
const browser = await browserService.newBrowser({ headless: false });
|
|
10
|
-
const
|
|
10
|
+
const context = await browser.newContext();
|
|
11
|
+
const page = await context.newPage();
|
|
11
12
|
await page.navigate("https://google.com");
|
|
12
|
-
await page.
|
|
13
|
+
await page.getBySelector("//div[text() = 'Alle ablehnen']").click();
|
|
13
14
|
await (0, import_timing.timeout)(1e3);
|
|
14
15
|
const pdf = await page.renderPdf();
|
|
15
16
|
(0, import_node_fs.writeFileSync)("/tmp/pdf.pdf", pdf);
|
|
16
17
|
await page.navigate("file:///tmp/pdf.pdf");
|
|
17
|
-
await
|
|
18
|
+
await page.waitForClose();
|
|
18
19
|
}
|
|
19
20
|
import_application.Application.run(main);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tstdl/base",
|
|
3
|
-
"version": "0.84.
|
|
3
|
+
"version": "0.84.3",
|
|
4
4
|
"author": "Patrick Hein",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"luxon": "^3.3",
|
|
24
24
|
"reflect-metadata": "^0.1",
|
|
25
25
|
"rxjs": "^7.8",
|
|
26
|
-
"type-fest": "^3.
|
|
26
|
+
"type-fest": "^3.10"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/chroma-js": "2.4",
|
|
@@ -31,13 +31,13 @@
|
|
|
31
31
|
"@types/luxon": "3.3",
|
|
32
32
|
"@types/minio": "7.0",
|
|
33
33
|
"@types/mjml": "4.7",
|
|
34
|
-
"@types/node": "
|
|
34
|
+
"@types/node": "20",
|
|
35
35
|
"@types/nodemailer": "6.4",
|
|
36
36
|
"@typescript-eslint/eslint-plugin": "5.59",
|
|
37
37
|
"@typescript-eslint/parser": "5.59",
|
|
38
38
|
"concurrently": "8.0",
|
|
39
39
|
"esbuild": "0.17",
|
|
40
|
-
"eslint": "8.
|
|
40
|
+
"eslint": "8.40",
|
|
41
41
|
"eslint-import-resolver-typescript": "3.5",
|
|
42
42
|
"eslint-plugin-import": "2.27",
|
|
43
43
|
"tsc-alias": "1.8",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"koa": "^2.14",
|
|
59
59
|
"minio": "^7.1",
|
|
60
60
|
"mjml": "^4.14",
|
|
61
|
-
"mongodb": "^5.
|
|
61
|
+
"mongodb": "^5.4",
|
|
62
62
|
"nodemailer": "^6.9",
|
|
63
63
|
"playwright": "^1.33",
|
|
64
64
|
"preact": "^10.13",
|
package/pdf/pdf.service.js
CHANGED
|
@@ -153,10 +153,17 @@ let PdfService = class PdfService2 {
|
|
|
153
153
|
}
|
|
154
154
|
renderStream(handler, options = {}) {
|
|
155
155
|
return (0, import_readable_stream_from_promise.readableStreamFromPromise)(async () => {
|
|
156
|
-
const
|
|
156
|
+
const context = options.browserContext ?? await this.browserController.newContext({ locale: options.locale ?? this.defaultLocale });
|
|
157
|
+
const page = await context.newPage();
|
|
157
158
|
const optionsFromHandler = await handler(page);
|
|
158
159
|
const pdfStream = page.renderPdfStream({ ...optionsFromHandler, ...options });
|
|
159
|
-
const close = async () =>
|
|
160
|
+
const close = async () => {
|
|
161
|
+
if ((0, import_type_guards.isDefined)(options.browserContext)) {
|
|
162
|
+
await page.close();
|
|
163
|
+
} else {
|
|
164
|
+
await context.close();
|
|
165
|
+
}
|
|
166
|
+
};
|
|
160
167
|
return (0, import_finalize_stream.finalizeStream)(pdfStream, {
|
|
161
168
|
beforeDone: close,
|
|
162
169
|
beforeCancel: close,
|
|
@@ -32,7 +32,7 @@ class Uint8ArrayCoercer extends import_types.SchemaValueCoercer {
|
|
|
32
32
|
sourceType = Array;
|
|
33
33
|
targetType = Uint8Array;
|
|
34
34
|
coerce(value, path, { options }) {
|
|
35
|
-
const testResult = (0, import_schema.
|
|
35
|
+
const testResult = (0, import_schema.testSchema)(byteNumberArraySchema, value, options, path);
|
|
36
36
|
if (!testResult.valid) {
|
|
37
37
|
return { success: false, error: import_schema_error.SchemaError.couldNotCoerce(Uint8Array, Array, path, { inner: testResult.error, fast: options.fastErrors }) };
|
|
38
38
|
}
|
package/schema/schema.d.ts
CHANGED
|
@@ -11,4 +11,5 @@ export declare const Schema: {
|
|
|
11
11
|
function<T_3 extends readonly SchemaTestable<any>[], R extends SchemaTestable<any>, F extends (...args: TupleSchemaOutput<T_3>) => SchemaOutput<R>>(argumentSchemas: T_3, returnSchema: R, handler: F): F;
|
|
12
12
|
asyncFunction<T_4 extends readonly SchemaTestable<any>[], R_1 extends SchemaTestable<any>, F_1 extends (...args: TupleSchemaOutput<T_4>) => Promise<SchemaOutput<R_1>>>(argumentSchemas: T_4, returnSchema: R_1, handler: F_1): F_1;
|
|
13
13
|
};
|
|
14
|
-
export declare function
|
|
14
|
+
export declare function testSchema<T>(schema: Schema<T>, value: unknown, options?: SchemaTestOptions, path?: JsonPath): SchemaTestResult<T>;
|
|
15
|
+
export declare function getExpectString(schema: SchemaTestable): string;
|
package/schema/schema.error.js
CHANGED
|
@@ -24,6 +24,7 @@ module.exports = __toCommonJS(schema_error_exports);
|
|
|
24
24
|
var import_custom_error = require("../error/custom.error.js");
|
|
25
25
|
var import_array = require("../utils/array/array.js");
|
|
26
26
|
var import_type_guards = require("../utils/type-guards.js");
|
|
27
|
+
var import_schema = require("./schema.js");
|
|
27
28
|
var import_utils = require("./utils/index.js");
|
|
28
29
|
class SchemaError extends import_custom_error.CustomError {
|
|
29
30
|
static errorName = "SchemaError";
|
|
@@ -42,9 +43,9 @@ class SchemaError extends import_custom_error.CustomError {
|
|
|
42
43
|
}
|
|
43
44
|
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
|
|
44
45
|
static expectedButGot(expected, got, path, options) {
|
|
45
|
-
const expectedNames = (0, import_array.toArray)(expected).map((exp) => (0, import_type_guards.isString)(exp) ? exp : (0,
|
|
46
|
+
const expectedNames = (0, import_array.toArray)(expected).map((exp) => (0, import_type_guards.isString)(exp) ? exp : (0, import_schema.getExpectString)(exp));
|
|
46
47
|
const gotName = (0, import_type_guards.isString)(got) ? got : (0, import_utils.getValueTypeName)(got);
|
|
47
|
-
const expectedString = expectedNames.length == 1 ? expectedNames[0] : `
|
|
48
|
+
const expectedString = expectedNames.length == 1 ? expectedNames[0] : `(${expectedNames.join(" | ")})`;
|
|
48
49
|
const customMessage = (0, import_type_guards.isDefined)(options.customMessage) ? `: ${options.customMessage}` : ".";
|
|
49
50
|
const message = `Expected ${expectedString} but got ${gotName}${customMessage}`;
|
|
50
51
|
return new SchemaError(message, { path, ...options });
|
package/schema/schema.js
CHANGED
|
@@ -19,7 +19,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var schema_exports = {};
|
|
20
20
|
__export(schema_exports, {
|
|
21
21
|
Schema: () => Schema,
|
|
22
|
-
|
|
22
|
+
getExpectString: () => getExpectString,
|
|
23
|
+
testSchema: () => testSchema
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(schema_exports);
|
|
25
26
|
var import_json_path = require("../json-path/index.js");
|
|
@@ -54,14 +55,14 @@ const Schema = {
|
|
|
54
55
|
test(schemaOrValueType, value, options, path = import_json_path.JsonPath.ROOT) {
|
|
55
56
|
const normalizedOptions = { fastErrors: true, ...options };
|
|
56
57
|
const schema = (0, import_types.schemaTestableToSchema)(schemaOrValueType);
|
|
57
|
-
const result =
|
|
58
|
+
const result = testSchema(schema, value, normalizedOptions, path);
|
|
58
59
|
if (result.valid) {
|
|
59
60
|
return result;
|
|
60
61
|
}
|
|
61
|
-
if (
|
|
62
|
-
|
|
62
|
+
if ((0, import_type_guards.isUndefined)(result.error.stack)) {
|
|
63
|
+
result.error.stack = new Error().stack;
|
|
63
64
|
}
|
|
64
|
-
return
|
|
65
|
+
return result;
|
|
65
66
|
},
|
|
66
67
|
validate(schemaOrValueType, value, options) {
|
|
67
68
|
const schema = (0, import_types.schemaTestableToSchema)(schemaOrValueType);
|
|
@@ -107,7 +108,7 @@ function getFunctionParametersSchema(argumentSchemas) {
|
|
|
107
108
|
argumentSchemas.forEach((arg, index) => schema.properties[index] = arg);
|
|
108
109
|
return schema;
|
|
109
110
|
}
|
|
110
|
-
function
|
|
111
|
+
function testSchema(schema, value, options, path = import_json_path.JsonPath.ROOT) {
|
|
111
112
|
initialize();
|
|
112
113
|
if ((0, import_types.isValueSchema)(schema)) {
|
|
113
114
|
return testValue(schema, value, options, path);
|
|
@@ -122,9 +123,7 @@ function testWithFastError(schema, value, options, path = import_json_path.JsonP
|
|
|
122
123
|
}
|
|
123
124
|
function testType(schema, value, options = {}, path = import_json_path.JsonPath.ROOT) {
|
|
124
125
|
const resolvedValueType = (0, import_types.resolveValueType)(schema.type);
|
|
125
|
-
if (
|
|
126
|
-
return { valid: true, value };
|
|
127
|
-
} else if ((0, import_type_guards.isFunction)(resolvedValueType)) {
|
|
126
|
+
if ((0, import_type_guards.isFunction)(resolvedValueType)) {
|
|
128
127
|
if (value instanceof resolvedValueType || (0, import_utils.getValueType)(value) == resolvedValueType) {
|
|
129
128
|
return { valid: true, value };
|
|
130
129
|
}
|
|
@@ -132,7 +131,7 @@ function testType(schema, value, options = {}, path = import_json_path.JsonPath.
|
|
|
132
131
|
if ((0, import_type_guards.isNotNull)(objectSchema)) {
|
|
133
132
|
return testObject(objectSchema, value, options, path);
|
|
134
133
|
}
|
|
135
|
-
} else if (resolvedValueType == "null" && (0, import_type_guards.isNull)(value) || resolvedValueType == "undefined" && (0, import_type_guards.isUndefined)(value)
|
|
134
|
+
} else if (resolvedValueType == "any" || resolvedValueType == "null" && (0, import_type_guards.isNull)(value) || resolvedValueType == "undefined" && (0, import_type_guards.isUndefined)(value)) {
|
|
136
135
|
return { valid: true, value };
|
|
137
136
|
}
|
|
138
137
|
return { valid: false, error: import_schema_error.SchemaError.expectedButGot(resolvedValueType, (0, import_utils.getValueType)(value), path, { fast: options.fastErrors }) };
|
|
@@ -151,7 +150,7 @@ function testObject(objectSchema, value, options = {}, path = import_json_path.J
|
|
|
151
150
|
return { valid: false, error: new import_schema_error.SchemaError("Unknown property", { path: path.add(unknownValuePropertyKeys[0]), fast: options.fastErrors }) };
|
|
152
151
|
}
|
|
153
152
|
for (const key of schemaPropertyKeys) {
|
|
154
|
-
const propertyResult =
|
|
153
|
+
const propertyResult = testSchema(schema.properties[key], value[key], options, path.add(key));
|
|
155
154
|
if (!propertyResult.valid) {
|
|
156
155
|
return propertyResult;
|
|
157
156
|
}
|
|
@@ -161,12 +160,12 @@ function testObject(objectSchema, value, options = {}, path = import_json_path.J
|
|
|
161
160
|
for (const key of unknownValuePropertyKeys) {
|
|
162
161
|
const propertyPath = path.add(key);
|
|
163
162
|
if ((0, import_type_guards.isDefined)(schema.unknownPropertiesKey)) {
|
|
164
|
-
const keyResult =
|
|
163
|
+
const keyResult = testSchema(schema.unknownPropertiesKey, key, options);
|
|
165
164
|
if (!keyResult.valid && !mask) {
|
|
166
165
|
return { valid: false, error: new import_schema_error.SchemaError("Invalid property key.", { path: propertyPath, inner: keyResult.error, fast: options.fastErrors }) };
|
|
167
166
|
}
|
|
168
167
|
}
|
|
169
|
-
const propertyResult =
|
|
168
|
+
const propertyResult = testSchema(schema.unknownProperties, value[key], options, path.add(key));
|
|
170
169
|
if (!propertyResult.valid && !mask) {
|
|
171
170
|
return propertyResult;
|
|
172
171
|
}
|
|
@@ -275,7 +274,7 @@ function testValue(schema, value, options = {}, path = import_json_path.JsonPath
|
|
|
275
274
|
function isValidValue(validSchemas, value, options, path) {
|
|
276
275
|
const errorResults = [];
|
|
277
276
|
for (const schema of validSchemas) {
|
|
278
|
-
const result =
|
|
277
|
+
const result = testSchema(schema, value, options, path);
|
|
279
278
|
if (result.valid) {
|
|
280
279
|
return result;
|
|
281
280
|
}
|
|
@@ -287,15 +286,16 @@ function isValidValue(validSchemas, value, options, path) {
|
|
|
287
286
|
const errors = errorResults.map((result) => result.error);
|
|
288
287
|
const expectStrings = [];
|
|
289
288
|
for (const schema of validSchemas) {
|
|
290
|
-
getExpectString(schema);
|
|
289
|
+
expectStrings.push(getExpectString(schema));
|
|
291
290
|
}
|
|
292
|
-
const expectString = expectStrings.join("
|
|
291
|
+
const expectString = expectStrings.length > 1 ? `(${expectStrings.join(" | ")})` : expectStrings[0];
|
|
293
292
|
return { valid: false, error: import_schema_error.SchemaError.expectedButGot(expectString, (0, import_utils.getValueType)(value), path, { inner: errors, fast: options.fastErrors }) };
|
|
294
293
|
}
|
|
295
294
|
function getExpectString(schema) {
|
|
296
295
|
const expectedNames = (0, import_utils.getSchemaTypeNames)(schema);
|
|
297
|
-
const
|
|
296
|
+
const arraySuffix = (0, import_types.isValueSchema)(schema) && schema.array == true ? "[]" : "";
|
|
297
|
+
const expectedTypeString = expectedNames.length == 1 ? `${expectedNames[0]}${arraySuffix}` : `(${expectedNames.join(" | ")})${arraySuffix}`;
|
|
298
298
|
const expects = (0, import_types.isValueSchema)(schema) ? (0, import_array.toArray)(schema.valueConstraints ?? []).map((constraint) => constraint.expects) : [];
|
|
299
|
-
const expectsString = expects.length > 0 ? `
|
|
299
|
+
const expectsString = expects.length > 0 ? `[${expects.join(", ")}]` : "";
|
|
300
300
|
return `${expectedTypeString}${expectsString}`;
|
|
301
301
|
}
|
|
@@ -28,6 +28,7 @@ module.exports = __toCommonJS(value_type_exports);
|
|
|
28
28
|
var import_array = require("../../utils/array/array.js");
|
|
29
29
|
var import_type_guards = require("../../utils/type-guards.js");
|
|
30
30
|
var import_types = require("../types/types.js");
|
|
31
|
+
const lowercaseTypesSet = /* @__PURE__ */ new Set([...import_types.primitiveConstructorSet, Object, Array]);
|
|
31
32
|
function getValueType(value) {
|
|
32
33
|
if ((0, import_type_guards.isUndefined)(value)) {
|
|
33
34
|
return "undefined";
|
|
@@ -47,10 +48,10 @@ function includesValueType(valueType, valueTypes) {
|
|
|
47
48
|
}
|
|
48
49
|
function getValueTypeName(valueType) {
|
|
49
50
|
const resolvedValueType = (0, import_types.resolveValueType)(valueType);
|
|
50
|
-
return (0, import_type_guards.isString)(resolvedValueType) ? resolvedValueType : resolvedValueType.name;
|
|
51
|
+
return (0, import_type_guards.isString)(resolvedValueType) ? resolvedValueType : lowercaseTypesSet.has(resolvedValueType) ? resolvedValueType.name.toLowerCase() : resolvedValueType.name;
|
|
51
52
|
}
|
|
52
53
|
function getSchemaTypeNames(schema) {
|
|
53
|
-
return getSchemaValueTypes(schema).map(
|
|
54
|
+
return getSchemaValueTypes(schema).map(getValueTypeName);
|
|
54
55
|
}
|
|
55
56
|
function getSchemaValueTypes(schema) {
|
|
56
57
|
if ((0, import_types.isTypeSchema)(schema)) {
|