@webiny/api 0.0.0-unstable.9bd236cf5e β†’ 0.0.0-unstable.9f53ea597d

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.
Files changed (45) hide show
  1. package/Benchmark.d.ts +1 -1
  2. package/Benchmark.js +120 -156
  3. package/Benchmark.js.map +1 -1
  4. package/Context.d.ts +5 -5
  5. package/Context.js +61 -137
  6. package/Context.js.map +1 -1
  7. package/README.md +10 -14
  8. package/createConditionalPluginFactory.d.ts +1 -1
  9. package/createConditionalPluginFactory.js +5 -15
  10. package/createConditionalPluginFactory.js.map +1 -1
  11. package/decorateContext.js +10 -19
  12. package/decorateContext.js.map +1 -1
  13. package/helpers/InterfaceGenerator/date.d.ts +15 -0
  14. package/helpers/InterfaceGenerator/date.js +0 -0
  15. package/helpers/InterfaceGenerator/id.d.ts +23 -0
  16. package/helpers/InterfaceGenerator/id.js +0 -0
  17. package/helpers/InterfaceGenerator/identity.d.ts +10 -0
  18. package/helpers/InterfaceGenerator/identity.js +0 -0
  19. package/helpers/InterfaceGenerator/index.d.ts +6 -0
  20. package/helpers/InterfaceGenerator/index.js +0 -0
  21. package/helpers/InterfaceGenerator/numeric.d.ts +12 -0
  22. package/helpers/InterfaceGenerator/numeric.js +0 -0
  23. package/helpers/InterfaceGenerator/text.d.ts +11 -0
  24. package/helpers/InterfaceGenerator/text.js +0 -0
  25. package/helpers/InterfaceGenerator/truthful.d.ts +7 -0
  26. package/helpers/InterfaceGenerator/truthful.js +0 -0
  27. package/index.d.ts +5 -6
  28. package/index.js +5 -73
  29. package/package.json +19 -15
  30. package/plugins/BenchmarkPlugin.d.ts +1 -1
  31. package/plugins/BenchmarkPlugin.js +19 -27
  32. package/plugins/BenchmarkPlugin.js.map +1 -1
  33. package/plugins/ContextPlugin.d.ts +1 -1
  34. package/plugins/ContextPlugin.js +17 -26
  35. package/plugins/ContextPlugin.js.map +1 -1
  36. package/types.d.ts +5 -10
  37. package/types.js +0 -7
  38. package/ServiceDiscovery.d.ts +0 -12
  39. package/ServiceDiscovery.js +0 -77
  40. package/ServiceDiscovery.js.map +0 -1
  41. package/index.js.map +0 -1
  42. package/plugins/CompressorPlugin.d.ts +0 -14
  43. package/plugins/CompressorPlugin.js +0 -21
  44. package/plugins/CompressorPlugin.js.map +0 -1
  45. package/types.js.map +0 -1
package/Benchmark.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Benchmark as BenchmarkInterface, BenchmarkEnableOnCallable, BenchmarkMeasurement, BenchmarkMeasureOptions, BenchmarkOutputCallable, BenchmarkRuns } from "./types";
1
+ import type { Benchmark as BenchmarkInterface, BenchmarkEnableOnCallable, BenchmarkMeasurement, BenchmarkMeasureOptions, BenchmarkOutputCallable, BenchmarkRuns } from "./types.js";
2
2
  export declare class Benchmark implements BenchmarkInterface {
3
3
  readonly measurements: BenchmarkMeasurement[];
4
4
  private outputDone;
package/Benchmark.js CHANGED
@@ -1,160 +1,124 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Benchmark = void 0;
7
- var BenchmarkState = /*#__PURE__*/function (BenchmarkState) {
8
- BenchmarkState["DISABLED"] = "disabled";
9
- BenchmarkState["ENABLED"] = "enabled";
10
- BenchmarkState["UNDETERMINED"] = "undetermined";
11
- return BenchmarkState;
12
- }(BenchmarkState || {});
13
- const createDefaultOutputCallable = () => {
14
- return async ({
15
- benchmark
16
- }) => {
17
- console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);
18
- console.log("Benchmark measurements:");
19
- console.log(benchmark.measurements);
20
- };
21
- };
22
- class Benchmark {
23
- measurements = [];
24
- outputDone = false;
25
- isAlreadyRunning = false;
26
- totalElapsed = 0;
27
- runs = {};
28
- enableOnCallables = [];
29
- onOutputCallables = [];
30
- state = BenchmarkState.UNDETERMINED;
31
- get elapsed() {
32
- return this.totalElapsed;
33
- }
34
- enableOn(cb) {
35
- this.enableOnCallables.push(cb);
36
- }
37
- onOutput(cb) {
38
- this.onOutputCallables.push(cb);
39
- }
40
- enable() {
41
- this.setState(BenchmarkState.ENABLED);
42
- }
43
- disable() {
44
- this.setState(BenchmarkState.DISABLED);
45
- }
46
-
47
- /**
48
- * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.
49
- *
50
- * The last one is our built-in console.log output.
51
- */
52
- async output() {
53
- /**
54
- * No point in outputting more than once or if no measurements were made.
55
- */
56
- if (this.outputDone || this.measurements.length === 0) {
57
- return;
58
- }
59
- const callables = [...this.onOutputCallables].reverse();
60
- callables.push(createDefaultOutputCallable());
61
- for (const cb of callables) {
62
- const result = await cb({
63
- benchmark: this,
64
- stop: () => "stop"
65
- });
66
- if (result === "stop") {
67
- return;
68
- }
69
- }
70
- this.outputDone = true;
71
- }
72
- async measure(options, cb) {
73
- const enabled = await this.getIsEnabled();
74
- if (!enabled) {
75
- return cb();
76
- }
77
- const measurement = this.startMeasurement(options);
78
- const isAlreadyRunning = this.getIsAlreadyRunning();
79
- this.startRunning();
80
- try {
81
- return await cb();
82
- } finally {
83
- const measurementEnded = this.stopMeasurement(measurement);
84
- this.measurements.push(measurementEnded);
85
- this.addRun(measurementEnded);
86
- /**
87
- * Only add to total time if this run is not a child of another run.
88
- * And then end running.
89
- */
90
- if (!isAlreadyRunning) {
91
- this.addElapsed(measurementEnded);
92
- this.endRunning();
93
- }
94
- }
95
- }
96
- getIsAlreadyRunning() {
97
- return this.isAlreadyRunning;
98
- }
99
- startRunning() {
100
- this.isAlreadyRunning = true;
101
- }
102
- endRunning() {
103
- this.isAlreadyRunning = false;
104
- }
105
- async getIsEnabled() {
106
- if (this.state === BenchmarkState.ENABLED) {
107
- return true;
108
- } else if (this.state === BenchmarkState.DISABLED) {
109
- return false;
110
- }
111
- for (const cb of this.enableOnCallables) {
112
- const result = await cb();
113
- if (result) {
114
- this.enable();
115
- return true;
116
- }
117
- }
118
- this.disable();
119
- return false;
120
- }
121
- addElapsed(measurement) {
122
- this.totalElapsed = this.totalElapsed + measurement.elapsed;
123
- }
124
- addRun(measurement) {
125
- const name = `${measurement.category}#${measurement.name}`;
126
- if (!this.runs[name]) {
127
- this.runs[name] = 0;
128
- }
129
- this.runs[name]++;
130
- }
131
- setState(state) {
132
- this.state = state;
133
- }
134
- startMeasurement(options) {
135
- const name = typeof options === "string" ? options : options.name;
136
- const category = typeof options === "string" ? "webiny" : options.category;
137
- return {
138
- name,
139
- category,
140
- start: new Date(),
141
- memoryStart: process.memoryUsage().heapUsed
1
+ const createDefaultOutputCallable = ()=>async ({ benchmark })=>{
2
+ console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);
3
+ console.log("Benchmark measurements:");
4
+ console.log(benchmark.measurements);
142
5
  };
143
- }
144
- stopMeasurement(measurement) {
145
- const end = new Date();
146
- const memoryEnd = process.memoryUsage().heapUsed;
147
- const elapsed = end.getTime() - measurement.start.getTime();
148
- return {
149
- name: measurement.name,
150
- category: measurement.category,
151
- start: measurement.start,
152
- end,
153
- elapsed,
154
- memory: memoryEnd - measurement.memoryStart
155
- };
156
- }
6
+ class Benchmark {
7
+ get elapsed() {
8
+ return this.totalElapsed;
9
+ }
10
+ enableOn(cb) {
11
+ this.enableOnCallables.push(cb);
12
+ }
13
+ onOutput(cb) {
14
+ this.onOutputCallables.push(cb);
15
+ }
16
+ enable() {
17
+ this.setState("enabled");
18
+ }
19
+ disable() {
20
+ this.setState("disabled");
21
+ }
22
+ async output() {
23
+ if (this.outputDone || 0 === this.measurements.length) return;
24
+ const callables = [
25
+ ...this.onOutputCallables
26
+ ].reverse();
27
+ callables.push(createDefaultOutputCallable());
28
+ for (const cb of callables){
29
+ const result = await cb({
30
+ benchmark: this,
31
+ stop: ()=>"stop"
32
+ });
33
+ if ("stop" === result) return;
34
+ }
35
+ this.outputDone = true;
36
+ }
37
+ async measure(options, cb) {
38
+ const enabled = await this.getIsEnabled();
39
+ if (!enabled) return cb();
40
+ const measurement = this.startMeasurement(options);
41
+ const isAlreadyRunning = this.getIsAlreadyRunning();
42
+ this.startRunning();
43
+ try {
44
+ return await cb();
45
+ } finally{
46
+ const measurementEnded = this.stopMeasurement(measurement);
47
+ this.measurements.push(measurementEnded);
48
+ this.addRun(measurementEnded);
49
+ if (!isAlreadyRunning) {
50
+ this.addElapsed(measurementEnded);
51
+ this.endRunning();
52
+ }
53
+ }
54
+ }
55
+ getIsAlreadyRunning() {
56
+ return this.isAlreadyRunning;
57
+ }
58
+ startRunning() {
59
+ this.isAlreadyRunning = true;
60
+ }
61
+ endRunning() {
62
+ this.isAlreadyRunning = false;
63
+ }
64
+ async getIsEnabled() {
65
+ if ("enabled" === this.state) return true;
66
+ if ("disabled" === this.state) return false;
67
+ for (const cb of this.enableOnCallables){
68
+ const result = await cb();
69
+ if (result) {
70
+ this.enable();
71
+ return true;
72
+ }
73
+ }
74
+ this.disable();
75
+ return false;
76
+ }
77
+ addElapsed(measurement) {
78
+ this.totalElapsed = this.totalElapsed + measurement.elapsed;
79
+ }
80
+ addRun(measurement) {
81
+ const name = `${measurement.category}#${measurement.name}`;
82
+ if (!this.runs[name]) this.runs[name] = 0;
83
+ this.runs[name]++;
84
+ }
85
+ setState(state) {
86
+ this.state = state;
87
+ }
88
+ startMeasurement(options) {
89
+ const name = "string" == typeof options ? options : options.name;
90
+ const category = "string" == typeof options ? "webiny" : options.category;
91
+ return {
92
+ name,
93
+ category,
94
+ start: new Date(),
95
+ memoryStart: process.memoryUsage().heapUsed
96
+ };
97
+ }
98
+ stopMeasurement(measurement) {
99
+ const end = new Date();
100
+ const memoryEnd = process.memoryUsage().heapUsed;
101
+ const elapsed = end.getTime() - measurement.start.getTime();
102
+ return {
103
+ name: measurement.name,
104
+ category: measurement.category,
105
+ start: measurement.start,
106
+ end,
107
+ elapsed,
108
+ memory: memoryEnd - measurement.memoryStart
109
+ };
110
+ }
111
+ constructor(){
112
+ this.measurements = [];
113
+ this.outputDone = false;
114
+ this.isAlreadyRunning = false;
115
+ this.totalElapsed = 0;
116
+ this.runs = {};
117
+ this.enableOnCallables = [];
118
+ this.onOutputCallables = [];
119
+ this.state = "undetermined";
120
+ }
157
121
  }
158
- exports.Benchmark = Benchmark;
122
+ export { Benchmark };
159
123
 
160
124
  //# sourceMappingURL=Benchmark.js.map
package/Benchmark.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["BenchmarkState","createDefaultOutputCallable","benchmark","console","log","elapsed","measurements","Benchmark","outputDone","isAlreadyRunning","totalElapsed","runs","enableOnCallables","onOutputCallables","state","UNDETERMINED","enableOn","cb","push","onOutput","enable","setState","ENABLED","disable","DISABLED","output","length","callables","reverse","result","stop","measure","options","enabled","getIsEnabled","measurement","startMeasurement","getIsAlreadyRunning","startRunning","measurementEnded","stopMeasurement","addRun","addElapsed","endRunning","name","category","start","Date","memoryStart","process","memoryUsage","heapUsed","end","memoryEnd","getTime","memory","exports"],"sources":["Benchmark.ts"],"sourcesContent":["import type {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart\n extends Pick<BenchmarkMeasurement, \"name\" | \"category\" | \"start\"> {\n memoryStart: number;\n}\n\nconst createDefaultOutputCallable = (): BenchmarkOutputCallable => {\n return async ({ benchmark }) => {\n console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(benchmark.measurements);\n };\n};\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The last one is our built-in console.log output.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = [...this.onOutputCallables].reverse();\n callables.push(createDefaultOutputCallable());\n for (const cb of callables) {\n const result = await cb({\n benchmark: this,\n stop: () => \"stop\"\n });\n if (result === \"stop\") {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(options);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(measurement: Pick<BenchmarkMeasurement, \"name\" | \"category\">): void {\n const name = `${measurement.category}#${measurement.name}`;\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(options: BenchmarkMeasureOptions | string): BenchmarkMeasurementStart {\n const name = typeof options === \"string\" ? options : options.name;\n const category = typeof options === \"string\" ? \"webiny\" : options.category;\n return {\n name,\n category,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n category: measurement.category,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n"],"mappings":";;;;;;IASKA,cAAc,0BAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAdA,cAAc;EAAA,OAAdA,cAAc;AAAA,EAAdA,cAAc;AAWnB,MAAMC,2BAA2B,GAAGA,CAAA,KAA+B;EAC/D,OAAO,OAAO;IAAEC;EAAU,CAAC,KAAK;IAC5BC,OAAO,CAACC,GAAG,CAAC,iCAAiCF,SAAS,CAACG,OAAO,IAAI,CAAC;IACnEF,OAAO,CAACC,GAAG,CAAC,yBAAyB,CAAC;IACtCD,OAAO,CAACC,GAAG,CAACF,SAAS,CAACI,YAAY,CAAC;EACvC,CAAC;AACL,CAAC;AAEM,MAAMC,SAAS,CAA+B;EACjCD,YAAY,GAA2B,EAAE;EAEjDE,UAAU,GAAG,KAAK;EAClBC,gBAAgB,GAAG,KAAK;EACxBC,YAAY,GAAG,CAAC;EACRC,IAAI,GAAkB,CAAC,CAAC;EACvBC,iBA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AC,CAAC;IAC3D,OAAO;MACHV,IAAI,EAAET,WAAW,CAACS,IAAI;MACtBC,QAAQ,EAAEV,WAAW,CAACU,QAAQ;MAC9BC,KAAK,EAAEX,WAAW,CAACW,KAAK;MACxBM,GAAG;MACH/C,OAAO;MACPkD,MAAM,EAAEF,SAAS,GAAGlB,WAAW,CAACa;IACpC,CAAC;EACL;AACJ;AAACQ,OAAA,CAAAjD,SAAA,GAAAA,SAAA","ignoreList":[]}
1
+ {"version":3,"file":"Benchmark.js","sources":["../src/Benchmark.ts"],"sourcesContent":["import type {\n Benchmark as BenchmarkInterface,\n BenchmarkEnableOnCallable,\n BenchmarkMeasurement,\n BenchmarkMeasureOptions,\n BenchmarkOutputCallable,\n BenchmarkRuns\n} from \"~/types.js\";\n\nenum BenchmarkState {\n DISABLED = \"disabled\",\n ENABLED = \"enabled\",\n UNDETERMINED = \"undetermined\"\n}\n\ninterface BenchmarkMeasurementStart extends Pick<\n BenchmarkMeasurement,\n \"name\" | \"category\" | \"start\"\n> {\n memoryStart: number;\n}\n\nconst createDefaultOutputCallable = (): BenchmarkOutputCallable => {\n return async ({ benchmark }) => {\n console.log(`Benchmark total time elapsed: ${benchmark.elapsed}ms`);\n console.log(\"Benchmark measurements:\");\n console.log(benchmark.measurements);\n };\n};\n\nexport class Benchmark implements BenchmarkInterface {\n public readonly measurements: BenchmarkMeasurement[] = [];\n\n private outputDone = false;\n private isAlreadyRunning = false;\n private totalElapsed = 0;\n public readonly runs: BenchmarkRuns = {};\n private readonly enableOnCallables: BenchmarkEnableOnCallable[] = [];\n private readonly onOutputCallables: BenchmarkOutputCallable[] = [];\n private state: BenchmarkState = BenchmarkState.UNDETERMINED;\n\n public get elapsed(): number {\n return this.totalElapsed;\n }\n\n public enableOn(cb: BenchmarkEnableOnCallable): void {\n this.enableOnCallables.push(cb);\n }\n\n public onOutput(cb: BenchmarkOutputCallable): void {\n this.onOutputCallables.push(cb);\n }\n\n public enable(): void {\n this.setState(BenchmarkState.ENABLED);\n }\n\n public disable(): void {\n this.setState(BenchmarkState.DISABLED);\n }\n\n /**\n * When running the output, we need to reverse the callables array, so that the last one added is the first one executed.\n *\n * The last one is our built-in console.log output.\n */\n public async output(): Promise<void> {\n /**\n * No point in outputting more than once or if no measurements were made.\n */\n if (this.outputDone || this.measurements.length === 0) {\n return;\n }\n const callables = [...this.onOutputCallables].reverse();\n callables.push(createDefaultOutputCallable());\n for (const cb of callables) {\n const result = await cb({\n benchmark: this,\n stop: () => \"stop\"\n });\n if (result === \"stop\") {\n return;\n }\n }\n this.outputDone = true;\n }\n\n public async measure<T = any>(\n options: BenchmarkMeasureOptions | string,\n cb: () => Promise<T>\n ): Promise<T> {\n const enabled = await this.getIsEnabled();\n if (!enabled) {\n return cb();\n }\n const measurement = this.startMeasurement(options);\n const isAlreadyRunning = this.getIsAlreadyRunning();\n this.startRunning();\n try {\n return await cb();\n } finally {\n const measurementEnded = this.stopMeasurement(measurement);\n this.measurements.push(measurementEnded);\n this.addRun(measurementEnded);\n /**\n * Only add to total time if this run is not a child of another run.\n * And then end running.\n */\n if (!isAlreadyRunning) {\n this.addElapsed(measurementEnded);\n this.endRunning();\n }\n }\n }\n\n private getIsAlreadyRunning(): boolean {\n return this.isAlreadyRunning;\n }\n private startRunning(): void {\n this.isAlreadyRunning = true;\n }\n private endRunning(): void {\n this.isAlreadyRunning = false;\n }\n\n private async getIsEnabled(): Promise<boolean> {\n if (this.state === BenchmarkState.ENABLED) {\n return true;\n } else if (this.state === BenchmarkState.DISABLED) {\n return false;\n }\n\n for (const cb of this.enableOnCallables) {\n const result = await cb();\n if (result) {\n this.enable();\n return true;\n }\n }\n this.disable();\n return false;\n }\n\n private addElapsed(measurement: Pick<BenchmarkMeasurement, \"elapsed\">): void {\n this.totalElapsed = this.totalElapsed + measurement.elapsed;\n }\n\n private addRun(measurement: Pick<BenchmarkMeasurement, \"name\" | \"category\">): void {\n const name = `${measurement.category}#${measurement.name}`;\n if (!this.runs[name]) {\n this.runs[name] = 0;\n }\n this.runs[name]++;\n }\n\n private setState(state: BenchmarkState): void {\n this.state = state;\n }\n\n private startMeasurement(options: BenchmarkMeasureOptions | string): BenchmarkMeasurementStart {\n const name = typeof options === \"string\" ? options : options.name;\n const category = typeof options === \"string\" ? \"webiny\" : options.category;\n return {\n name,\n category,\n start: new Date(),\n memoryStart: process.memoryUsage().heapUsed\n };\n }\n\n private stopMeasurement(measurement: BenchmarkMeasurementStart): BenchmarkMeasurement {\n const end = new Date();\n const memoryEnd = process.memoryUsage().heapUsed;\n const elapsed = end.getTime() - measurement.start.getTime();\n return {\n name: measurement.name,\n category: measurement.category,\n start: measurement.start,\n end,\n elapsed,\n memory: memoryEnd - measurement.memoryStart\n };\n }\n}\n"],"names":["createDefaultOutputCallable","benchmark","console","Benchmark","cb","callables","result","options","enabled","measurement","isAlreadyRunning","measurementEnded","name","state","category","Date","process","end","memoryEnd","elapsed"],"mappings":"AAsBA,MAAMA,8BAA8B,IACzB,OAAO,EAAEC,SAAS,EAAE;QACvBC,QAAQ,GAAG,CAAC,CAAC,8BAA8B,EAAED,UAAU,OAAO,CAAC,EAAE,CAAC;QAClEC,QAAQ,GAAG,CAAC;QACZA,QAAQ,GAAG,CAACD,UAAU,YAAY;IACtC;AAGG,MAAME;IAWT,IAAW,UAAkB;QACzB,OAAO,IAAI,CAAC,YAAY;IAC5B;IAEO,SAASC,EAA6B,EAAQ;QACjD,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAACA;IAChC;IAEO,SAASA,EAA2B,EAAQ;QAC/C,IAAI,CAAC,iBAAiB,CAAC,IAAI,CAACA;IAChC;IAEO,SAAe;QAClB,IAAI,CAAC,QAAQ,CAAC;IAClB;IAEO,UAAgB;QACnB,IAAI,CAAC,QAAQ,CAAC;IAClB;IAOA,MAAa,SAAwB;QAIjC,IAAI,IAAI,CAAC,UAAU,IAAI,AAA6B,MAA7B,IAAI,CAAC,YAAY,CAAC,MAAM,EAC3C;QAEJ,MAAMC,YAAY;eAAI,IAAI,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Context as ContextInterface } from "./types.js";
2
2
  import { PluginsContainer } from "@webiny/plugins";
3
- import type { PluginCollection } from "@webiny/plugins/types";
4
- import { Benchmark } from "./Benchmark";
5
- import type { ICompressor } from "@webiny/utils/compression/Compressor";
3
+ import type { PluginCollection } from "@webiny/plugins/types.js";
4
+ import { Benchmark } from "./Benchmark.js";
5
+ import { Container } from "@webiny/di";
6
6
  export interface ContextParams {
7
7
  plugins?: PluginCollection | PluginsContainer;
8
8
  WEBINY_VERSION: string;
@@ -13,7 +13,7 @@ export declare class Context implements ContextInterface {
13
13
  readonly plugins: PluginsContainer;
14
14
  readonly WEBINY_VERSION: string;
15
15
  readonly benchmark: Benchmark;
16
- readonly compressor: ICompressor;
16
+ readonly container: Container;
17
17
  private readonly waiters;
18
18
  constructor(params: ContextParams);
19
19
  getResult(): any;
package/Context.js CHANGED
@@ -1,144 +1,68 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.Context = void 0;
7
- var _plugins = require("@webiny/plugins");
8
- var _Benchmark = require("./Benchmark");
9
- var _BenchmarkPlugin = require("./plugins/BenchmarkPlugin");
10
- var _compression = require("@webiny/utils/compression");
11
- var _CompressorPlugin = require("./plugins/CompressorPlugin");
12
- const getPluginsContainer = plugins => {
13
- if (!plugins) {
14
- return new _plugins.PluginsContainer();
15
- }
16
- if (plugins instanceof _plugins.PluginsContainer) {
17
- return plugins;
18
- }
19
- return new _plugins.PluginsContainer(plugins);
1
+ import { PluginsContainer } from "@webiny/plugins";
2
+ import { Benchmark } from "./Benchmark.js";
3
+ import { BenchmarkPlugin } from "./plugins/BenchmarkPlugin.js";
4
+ import { Container } from "@webiny/di";
5
+ import { CompressionFeature } from "@webiny/utils/features/compression/feature.js";
6
+ const getPluginsContainer = (plugins)=>{
7
+ if (!plugins) return new PluginsContainer();
8
+ if (plugins instanceof PluginsContainer) return plugins;
9
+ return new PluginsContainer(plugins);
20
10
  };
21
11
  class Context {
22
- waiters = [];
23
- constructor(params) {
24
- const {
25
- plugins,
26
- WEBINY_VERSION
27
- } = params;
28
- this.plugins = getPluginsContainer(plugins);
29
- this.WEBINY_VERSION = WEBINY_VERSION;
30
- /**
31
- * At the moment let's have benchmark as part of the context.
32
- * Also, register the plugin to have benchmark accessible via plugins container.
33
- */
34
- this.benchmark = new _Benchmark.Benchmark();
35
- this.compressor = (0, _compression.createDefaultCompressor)({
36
- plugins: this.plugins
37
- });
38
- this.plugins.register(new _BenchmarkPlugin.BenchmarkPlugin(this.benchmark), new _CompressorPlugin.CompressorPlugin({
39
- getCompressor: () => {
40
- return this.compressor;
41
- }
42
- }));
43
- }
44
- getResult() {
45
- return this._result;
46
- }
47
- hasResult() {
48
- return !!this._result;
49
- }
50
- setResult(value) {
51
- this._result = value;
52
- }
53
- waitFor(obj, cb) {
54
- const initialTargets = Array.isArray(obj) ? obj : [obj];
55
- const targets = [];
56
- /**
57
- * We go only through the first level properties
58
- */
59
- for (const key in initialTargets) {
60
- const target = initialTargets[key];
61
- /**
62
- * If property already exists, there is no need to wait for it, so we just continue the loop.
63
- * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.
64
- */
65
- if (this[target]) {
66
- continue;
67
- } else if (typeof target !== "string") {
68
- continue;
69
- }
70
- /**
71
- * Since there is no property, we must define it with its setter and getter.
72
- * We could not know when it got defined otherwise.
73
- */
74
- Object.defineProperty(this, target, {
75
- /**
76
- * Setter sets the given value to this object.
77
- * We cannot set it on exact property name it is defined because it would go into loop of setting itself.
78
- * And that is why we add __ around the property name.
79
- */
80
- set: value => {
81
- const newTargetKey = `__${target}__`;
82
- this[newTargetKey] = value;
83
- /**
84
- * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.
85
- */
86
- for (const waiter of this.waiters) {
87
- if (waiter.targets.includes(target) === false) {
88
- continue;
89
- }
90
- /**
91
- * Remove currently set property so we know if there are any more to be waited for.
92
- */
93
- waiter.targets = waiter.targets.filter(t => t !== target);
94
- /**
95
- * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.
96
- */
97
- if (waiter.targets.length > 0) {
98
- continue;
99
- }
100
- /**
101
- * And if there is nothing more to be waited for, we execute the callable.
102
- * Note that this callable is not async.
103
- */
104
- waiter.cb(this);
105
- }
106
- },
107
- /**
108
- * As we have set property with __ around it, we must get it as well.
109
- */
110
- get: () => {
111
- const newTargetKey = `__${target}__`;
112
- return this[newTargetKey];
113
- },
114
- configurable: false
115
- });
116
- /**
117
- * We add the target to be awaited.
118
- */
119
- targets.push(target);
12
+ constructor(params){
13
+ this.waiters = [];
14
+ const { plugins, WEBINY_VERSION } = params;
15
+ this.plugins = getPluginsContainer(plugins);
16
+ this.WEBINY_VERSION = WEBINY_VERSION;
17
+ this.container = new Container();
18
+ CompressionFeature.register(this.container);
19
+ this.benchmark = new Benchmark();
20
+ this.plugins.register(new BenchmarkPlugin(this.benchmark));
21
+ }
22
+ getResult() {
23
+ return this._result;
120
24
  }
121
- /**
122
- * If there are no targets to be awaited, just fire the callable.
123
- */
124
- if (targets.length === 0) {
125
- cb(this);
126
- return;
25
+ hasResult() {
26
+ return !!this._result;
27
+ }
28
+ setResult(value) {
29
+ this._result = value;
30
+ }
31
+ waitFor(obj, cb) {
32
+ const initialTargets = Array.isArray(obj) ? obj : [
33
+ obj
34
+ ];
35
+ const targets = [];
36
+ for(const key in initialTargets){
37
+ const target = initialTargets[key];
38
+ if (!this[target]) {
39
+ if ("string" == typeof target) {
40
+ Object.defineProperty(this, target, {
41
+ set: (value)=>{
42
+ const newTargetKey = `__${target}__`;
43
+ this[newTargetKey] = value;
44
+ for (const waiter of this.waiters)if (false !== waiter.targets.includes(target)) {
45
+ waiter.targets = waiter.targets.filter((t)=>t !== target);
46
+ if (!(waiter.targets.length > 0)) waiter.cb(this);
47
+ }
48
+ },
49
+ get: ()=>{
50
+ const newTargetKey = `__${target}__`;
51
+ return this[newTargetKey];
52
+ },
53
+ configurable: false
54
+ });
55
+ targets.push(target);
56
+ }
57
+ }
58
+ }
59
+ if (0 === targets.length) return void cb(this);
60
+ this.waiters.push({
61
+ targets,
62
+ cb
63
+ });
127
64
  }
128
- /**
129
- * Otherwise add the waiter for the target properties.
130
- */
131
- this.waiters.push({
132
- targets,
133
- /**
134
- * TODO @ts-refactor
135
- * Problem with possible subtype initialization
136
- */
137
- // @ts-expect-error
138
- cb
139
- });
140
- }
141
65
  }
142
- exports.Context = Context;
66
+ export { Context };
143
67
 
144
68
  //# sourceMappingURL=Context.js.map
package/Context.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"names":["_plugins","require","_Benchmark","_BenchmarkPlugin","_compression","_CompressorPlugin","getPluginsContainer","plugins","PluginsContainer","Context","waiters","constructor","params","WEBINY_VERSION","benchmark","Benchmark","compressor","createDefaultCompressor","register","BenchmarkPlugin","CompressorPlugin","getCompressor","getResult","_result","hasResult","setResult","value","waitFor","obj","cb","initialTargets","Array","isArray","targets","key","target","Object","defineProperty","set","newTargetKey","waiter","includes","filter","t","length","get","configurable","push","exports"],"sources":["Context.ts"],"sourcesContent":["import type { Context as ContextInterface } from \"~/types\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport type { PluginCollection } from \"@webiny/plugins/types\";\nimport { Benchmark } from \"~/Benchmark\";\nimport { BenchmarkPlugin } from \"~/plugins/BenchmarkPlugin\";\nimport type { ICompressor } from \"@webiny/utils/compression/Compressor\";\nimport { createDefaultCompressor } from \"@webiny/utils/compression\";\nimport { CompressorPlugin } from \"~/plugins/CompressorPlugin\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection | PluginsContainer;\n WEBINY_VERSION: string;\n}\n\nconst getPluginsContainer = (plugins?: PluginCollection | PluginsContainer): PluginsContainer => {\n if (!plugins) {\n return new PluginsContainer();\n }\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins);\n};\n\nexport class Context implements ContextInterface {\n public _result: any;\n public args: any;\n public readonly plugins: PluginsContainer;\n public readonly WEBINY_VERSION: string;\n public readonly benchmark: Benchmark;\n public readonly compressor: ICompressor;\n\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = getPluginsContainer(plugins);\n this.WEBINY_VERSION = WEBINY_VERSION;\n /**\n * At the moment let's have benchmark as part of the context.\n * Also, register the plugin to have benchmark accessible via plugins container.\n */\n this.benchmark = new Benchmark();\n this.compressor = createDefaultCompressor({\n plugins: this.plugins\n });\n this.plugins.register(\n new BenchmarkPlugin(this.benchmark),\n new CompressorPlugin({\n getCompressor: () => {\n return this.compressor;\n }\n })\n );\n }\n\n public getResult(): any {\n return this._result;\n }\n\n public hasResult(): boolean {\n return !!this._result;\n }\n\n public setResult(value: any): void {\n this._result = value;\n }\n\n public waitFor<T extends ContextInterface = ContextInterface>(\n obj: string | string[],\n cb: (context: T) => void\n ): void {\n const initialTargets = Array.isArray(obj) ? obj : [obj];\n const targets: string[] = [];\n /**\n * We go only through the first level properties\n */\n for (const key in initialTargets) {\n const target = initialTargets[key] as keyof this;\n /**\n * If property already exists, there is no need to wait for it, so we just continue the loop.\n * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.\n */\n if (this[target]) {\n continue;\n } else if (typeof target !== \"string\") {\n continue;\n }\n /**\n * Since there is no property, we must define it with its setter and getter.\n * We could not know when it got defined otherwise.\n */\n Object.defineProperty(this, target, {\n /**\n * Setter sets the given value to this object.\n * We cannot set it on exact property name it is defined because it would go into loop of setting itself.\n * And that is why we add __ around the property name.\n */\n set: (value: any) => {\n const newTargetKey = `__${target}__` as keyof this;\n this[newTargetKey] = value;\n /**\n * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.\n */\n for (const waiter of this.waiters) {\n if (waiter.targets.includes(target) === false) {\n continue;\n }\n /**\n * Remove currently set property so we know if there are any more to be waited for.\n */\n waiter.targets = waiter.targets.filter(t => t !== target);\n /**\n * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.\n */\n if (waiter.targets.length > 0) {\n continue;\n }\n /**\n * And if there is nothing more to be waited for, we execute the callable.\n * Note that this callable is not async.\n */\n waiter.cb(this);\n }\n },\n /**\n * As we have set property with __ around it, we must get it as well.\n */\n get: (): any => {\n const newTargetKey = `__${target}__` as keyof this;\n return this[newTargetKey];\n },\n configurable: false\n });\n /**\n * We add the target to be awaited.\n */\n targets.push(target as string);\n }\n /**\n * If there are no targets to be awaited, just fire the callable.\n */\n if (targets.length === 0) {\n cb(this as unknown as T);\n return;\n }\n /**\n * Otherwise add the waiter for the target properties.\n */\n this.waiters.push({\n targets,\n /**\n * TODO @ts-refactor\n * Problem with possible subtype initialization\n */\n // @ts-expect-error\n cb\n });\n }\n}\n"],"mappings":";;;;;;AACA,IAAAA,QAAA,GAAAC,OAAA;AAEA,IAAAC,UAAA,GAAAD,OAAA;AACA,IAAAE,gBAAA,GAAAF,OAAA;AAEA,IAAAG,YAAA,GAAAH,OAAA;AACA,IAAAI,iBAAA,GAAAJ,OAAA;AAYA,MAAMK,mBAAmB,GAAIC,OAA6C,IAAuB;EAC7F,IAAI,CAACA,OAAO,EAAE;IACV,OAAO,IAAIC,yBAAgB,CAAC,CAAC;EACjC;EACA,IAAID,OAAO,YAAYC,yBAAgB,EAAE;IACrC,OAAOD,OAAO;EAClB;EACA,OAAO,IAAIC,yBAAgB,CAACD,OAAO,CAAC;AACxC,CAAC;AAEM,MAAME,OAAO,CAA6B;EAQ5BC,OAAO,GAAa,EAAE;EAEhCC,WAAWA,CAACC,MAAqB,EAAE;IACtC,MAAM;MAAEL,OAAO;MAAEM;IAAe,CAAC,GAAGD,MAAM;IAC1C,IAAI,CAACL,OAAO,GAAGD,mBAAmB,CAACC,OAAO,CAAC;IAC3C,IAAI,CAACM,cAAc,GAAGA,cAAc;IACpC;AACR;AACA;AACA;IACQ,IAAI,CAACC,SAAS,GAAG,IAAIC,oBAAS,CAAC,CAAC;IAChC,IA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AAC9B,OAAO,EAAE;YAC/B,IAAI8B,MAAM,CAACP,OAAO,CAACQ,QAAQ,CAACN,MAAM,CAAC,KAAK,KAAK,EAAE;cAC3C;YACJ;YACA;AACxB;AACA;YACwBK,MAAM,CAACP,OAAO,GAAGO,MAAM,CAACP,OAAO,CAACS,MAAM,CAACC,CAAC,IAAIA,CAAC,KAAKR,MAAM,CAAC;YACzD;AACxB;AACA;YACwB,IAAIK,MAAM,CAACP,OAAO,CAACW,MAAM,GAAG,CAAC,EAAE;cAC3B;YACJ;YACA;AACxB;AACA;AACA;YACwBJ,MAAM,CAACX,EAAE,CAAC,IAAI,CAAC;UACnB;QACJ,CAAC;QACD;AAChB;AACA;QACgBgB,GAAG,EAAEA,CAAA,KAAW;UACZ,MAAMN,YAAY,GAAG,KAAKJ,MAAM,IAAkB;UAClD,OAAO,IAAI,CAACI,YAAY,CAAC;QAC7B,CAAC;QACDO,YAAY,EAAE;MAClB,CAAC,CAAC;MACF;AACZ;AACA;MACYb,OAAO,CAACc,IAAI,CAACZ,MAAgB,CAAC;IAClC;IACA;AACR;AACA;IACQ,IAAIF,OAAO,CAACW,MAAM,KAAK,CAAC,EAAE;MACtBf,EAAE,CAAC,IAAoB,CAAC;MACxB;IACJ;IACA;AACR;AACA;IACQ,IAAI,CAACnB,OAAO,CAACqC,IAAI,CAAC;MACdd,OAAO;MACP;AACZ;AACA;AACA;MACY;MACAJ;IACJ,CAAC,CAAC;EACN;AACJ;AAACmB,OAAA,CAAAvC,OAAA,GAAAA,OAAA","ignoreList":[]}
1
+ {"version":3,"file":"Context.js","sources":["../src/Context.ts"],"sourcesContent":["import type { Context as ContextInterface } from \"~/types.js\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport type { PluginCollection } from \"@webiny/plugins/types.js\";\nimport { Benchmark } from \"~/Benchmark.js\";\nimport { BenchmarkPlugin } from \"~/plugins/BenchmarkPlugin.js\";\nimport { Container } from \"@webiny/di\";\nimport { CompressionFeature } from \"@webiny/utils/features/compression/feature.js\";\n\ninterface Waiter {\n targets: string[];\n cb: (context: ContextInterface) => void;\n}\n\nexport interface ContextParams {\n plugins?: PluginCollection | PluginsContainer;\n WEBINY_VERSION: string;\n}\n\nconst getPluginsContainer = (plugins?: PluginCollection | PluginsContainer): PluginsContainer => {\n if (!plugins) {\n return new PluginsContainer();\n }\n if (plugins instanceof PluginsContainer) {\n return plugins;\n }\n return new PluginsContainer(plugins);\n};\n\nexport class Context implements ContextInterface {\n public _result: any;\n public args: any;\n public readonly plugins: PluginsContainer;\n public readonly WEBINY_VERSION: string;\n public readonly benchmark: Benchmark;\n public readonly container: Container;\n\n private readonly waiters: Waiter[] = [];\n\n public constructor(params: ContextParams) {\n const { plugins, WEBINY_VERSION } = params;\n this.plugins = getPluginsContainer(plugins);\n this.WEBINY_VERSION = WEBINY_VERSION;\n this.container = new Container();\n /**\n * Register base features.\n */\n CompressionFeature.register(this.container);\n /**\n * At the moment, let's have benchmark as part of the context.\n * Also, register the plugin to have benchmark accessible via plugins container.\n */\n this.benchmark = new Benchmark();\n this.plugins.register(new BenchmarkPlugin(this.benchmark));\n }\n\n public getResult(): any {\n return this._result;\n }\n\n public hasResult(): boolean {\n return !!this._result;\n }\n\n public setResult(value: any): void {\n this._result = value;\n }\n\n public waitFor<T extends ContextInterface = ContextInterface>(\n obj: string | string[],\n cb: (context: T) => void\n ): void {\n const initialTargets = Array.isArray(obj) ? obj : [obj];\n const targets: string[] = [];\n /**\n * We go only through the first level properties\n */\n for (const key in initialTargets) {\n const target = initialTargets[key] as keyof this;\n /**\n * If property already exists, there is no need to wait for it, so we just continue the loop.\n * Also, if target is not a string, skip this property as it will fail to convert properly during the runtime.\n */\n if (this[target]) {\n continue;\n } else if (typeof target !== \"string\") {\n continue;\n }\n /**\n * Since there is no property, we must define it with its setter and getter.\n * We could not know when it got defined otherwise.\n */\n Object.defineProperty(this, target, {\n /**\n * Setter sets the given value to this object.\n * We cannot set it on exact property name it is defined because it would go into loop of setting itself.\n * And that is why we add __ around the property name.\n */\n set: (value: any) => {\n const newTargetKey = `__${target}__` as keyof this;\n this[newTargetKey] = value;\n /**\n * WWhen the property is set, we will go through all the waiters and, if any of them include currently set property, act on it.\n */\n for (const waiter of this.waiters) {\n if (waiter.targets.includes(target) === false) {\n continue;\n }\n /**\n * Remove currently set property so we know if there are any more to be waited for.\n */\n waiter.targets = waiter.targets.filter(t => t !== target);\n /**\n * If there are more to be waited, eg. user added [cms, pageBuilder] as waited properties, we just continue the loop.\n */\n if (waiter.targets.length > 0) {\n continue;\n }\n /**\n * And if there is nothing more to be waited for, we execute the callable.\n * Note that this callable is not async.\n */\n waiter.cb(this);\n }\n },\n /**\n * As we have set property with __ around it, we must get it as well.\n */\n get: (): any => {\n const newTargetKey = `__${target}__` as keyof this;\n return this[newTargetKey];\n },\n configurable: false\n });\n /**\n * We add the target to be awaited.\n */\n targets.push(target as string);\n }\n /**\n * If there are no targets to be awaited, just fire the callable.\n */\n if (targets.length === 0) {\n cb(this as unknown as T);\n return;\n }\n /**\n * Otherwise add the waiter for the target properties.\n */\n this.waiters.push({\n targets,\n /**\n * TODO @ts-refactor\n * Problem with possible subtype initialization\n */\n // @ts-expect-error\n cb\n });\n }\n}\n"],"names":["getPluginsContainer","plugins","PluginsContainer","Context","params","WEBINY_VERSION","Container","CompressionFeature","Benchmark","BenchmarkPlugin","value","obj","cb","initialTargets","Array","targets","key","target","Object","newTargetKey","waiter","t"],"mappings":";;;;;AAkBA,MAAMA,sBAAsB,CAACC;IACzB,IAAI,CAACA,SACD,OAAO,IAAIC;IAEf,IAAID,mBAAmBC,kBACnB,OAAOD;IAEX,OAAO,IAAIC,iBAAiBD;AAChC;AAEO,MAAME;IAUT,YAAmBC,MAAqB,CAAE;aAFzB,OAAO,GAAa,EAAE;QAGnC,MAAM,EAAEH,OAAO,EAAEI,cAAc,EAAE,GAAGD;QACpC,IAAI,CAAC,OAAO,GAAGJ,oBAAoBC;QACnC,IAAI,CAAC,cAAc,GAAGI;QACtB,IAA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AQ,IAAI,CAACE;;YAlDb;QAmDJ;QAIA,IAAIF,AAAmB,MAAnBA,QAAQ,MAAM,EAAQ,YACtBH,GAAG,IAAI;QAMX,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC;YACdG;YAMAH;QACJ;IACJ;AACJ"}
package/README.md CHANGED
@@ -1,15 +1,11 @@
1
1
  # @webiny/api
2
- [![](https://img.shields.io/npm/dw/@webiny/api.svg)](https://www.npmjs.com/package/@webiny/api)
3
- [![](https://img.shields.io/npm/v/@webiny/api.svg)](https://www.npmjs.com/package/@webiny/api)
4
- [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
5
- [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
6
-
7
- ## Install
8
- ```
9
- npm install --save @webiny/api
10
- ```
11
-
12
- Or if you prefer yarn:
13
- ```
14
- yarn add @webiny/api
15
- ```
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ πŸ“˜ **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -1,3 +1,3 @@
1
- import type { PluginFactory } from "@webiny/plugins/types";
1
+ import type { PluginFactory } from "@webiny/plugins/types.js";
2
2
  export type Condition = () => boolean;
3
3
  export declare const createConditionalPluginFactory: (condition: Condition, cb: PluginFactory) => PluginFactory;