Skip to content

Commit 5ba708b

Browse files
committed
wip:12 (h4980)
1 parent c631686 commit 5ba708b

File tree

8 files changed

+48
-14
lines changed

8 files changed

+48
-14
lines changed

extensions/MyApiKeyAfterUpdate.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
1-
import { ApiKeyAfterUpdateHandler } from "webiny/api/security/features/UpdateApiKey";
2-
1+
import { ApiKeyAfterUpdateHandler } from "webiny/api/security/apiKey";
32
import { Logger } from "webiny/api/logger";
3+
import { BuildParams } from "webiny/api/buildParams";
44

55
class MyApiKeyAfterUpdateImpl implements ApiKeyAfterUpdateHandler.Interface {
6-
constructor(private logger: Logger.Interface) {}
6+
constructor(
7+
private logger: Logger.Interface,
8+
private buildParams: BuildParams.Interface
9+
) {}
710

811
async handle() {
912
this.logger.warn("An API key was updated!");
13+
14+
// Read build params
15+
const param1 = this.buildParams.get<string>("MY_CUSTOM_BUILD_PARAM");
16+
const param2 = this.buildParams.get<{ myKey: number; nested: { foo: string } }>(
17+
"MY_CUSTOM_BUILD_PARAM-2"
18+
);
19+
20+
console.log("---- Build Params ----");
21+
22+
console.log(`Build param 1: ${param1}`);
23+
console.log(`Build param 2:`, param2);
1024
}
1125
}
1226

1327
const MyApiKeyAfterUpdate = ApiKeyAfterUpdateHandler.createImplementation({
1428
implementation: MyApiKeyAfterUpdateImpl,
15-
dependencies: [Logger]
29+
dependencies: [Logger, BuildParams]
1630
});
1731

1832
export default MyApiKeyAfterUpdate;
File renamed without changes.

packages/api-core/src/extensions/BuildParam.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ export const BuildParam = defineExtension({
1313
paramsSchema: () => {
1414
return z.object({
1515
paramName: z.string(),
16-
value: z.string()
16+
value: z.union([
17+
z.string(),
18+
z.record(z.any()),
19+
z.array(z.any()),
20+
z.number(),
21+
z.boolean()
22+
])
1723
});
1824
},
1925
async build(params, ctx) {
@@ -27,6 +33,9 @@ export const BuildParam = defineExtension({
2733

2834
const { paramName, value } = params;
2935

36+
// Serialize value to a TypeScript literal.
37+
const valueStr = JSON.stringify(value, null, 4);
38+
3039
// Generate a unique class name based on the paramName.
3140
const hash = crypto.createHash("sha256").update(paramName).digest("hex");
3241
const className = `BuildParam_${hash.slice(-10)}`;
@@ -43,12 +52,17 @@ export const BuildParam = defineExtension({
4352
// File exists, just ensure it's imported in extensions.ts
4453
} else {
4554
// Create the BuildParam implementation file.
46-
const fileContent = `import { BuildParam } from "@webiny/api-core/exports/api/buildParam";
55+
const fileContent = `import { BuildParam } from "webiny/api/buildParams";
4756
48-
export class ${className} implements BuildParam.Interface {
57+
class ${className} implements BuildParam.Interface {
4958
key = "${paramName}";
50-
value = "${value}";
59+
value = ${valueStr};
5160
}
61+
62+
export default BuildParam.createImplementation({
63+
implementation: ${className},
64+
dependencies: []
65+
});
5266
`;
5367
fs.writeFileSync(filePath, fileContent, "utf8");
5468
}
@@ -83,9 +97,9 @@ export class ${className} implements BuildParam.Interface {
8397
index = last.getChildIndex() + 1;
8498
}
8599

86-
// Add import for the BuildParam class.
100+
// Add import for the BuildParam implementation.
87101
source.insertImportDeclaration(index, {
88-
namedImports: [className],
102+
defaultImport: className,
89103
moduleSpecifier: importPath
90104
});
91105

packages/api-core/src/features/buildParams/BuildParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BuildParam, BuildParams as BuildParamsAbstraction } from "./abstraction
44
export class BuildParamsImpl implements BuildParamsAbstraction.Interface {
55
constructor(private container: Container) {}
66

7-
get(key: string): string | null {
7+
get<T = any>(key: string): T | null {
88
const params = this.container.resolveAll<BuildParam.Interface>(BuildParam);
99
const param = params.find(p => p.key === key);
1010
return param ? param.value : null;

packages/api-core/src/features/buildParams/abstractions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createAbstraction } from "@webiny/feature/api";
22

33
export interface IBuildParam {
44
key: string;
5-
value: string;
5+
value: any;
66
}
77

88
export const BuildParam = createAbstraction<IBuildParam>("BuildParam");
@@ -12,7 +12,7 @@ export namespace BuildParam {
1212
}
1313

1414
export interface IBuildParams {
15-
get(key: string): string | null;
15+
get<T = any>(key: string): T | null;
1616
}
1717

1818
export const BuildParams = createAbstraction<IBuildParams>("BuildParams");

packages/webiny/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"./configs/tsconfig.json": "./configs/tsconfig.json",
3838
"./global.ts": "./global.ts",
3939
"./admin/ui": "./admin/ui.js",
40+
"./api/buildParams": "./api/buildParams.js",
4041
"./api/eventPublisher": "./api/eventPublisher.js",
4142
"./api/keyValueStore": "./api/keyValueStore.js",
4243
"./api/logger": "./api/logger.js",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { BuildParam, BuildParams } from "@webiny/api-core/features/buildParams/index.js";

webiny.config.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export const Extensions = () => {
2323
<Infra.Aws.DefaultRegion name={"eu-central-1"} />
2424

2525
<Api.BuildParam paramName="MY_CUSTOM_BUILD_PARAM" value="customValue" />
26+
<Api.BuildParam
27+
paramName="MY_CUSTOM_BUILD_PARAM-2"
28+
value={{ myKey: 2, nested: { foo: "bar" } }}
29+
/>
2630
{/* Example: Environment-based conditional configuration */}
2731
{/*<Infra.Env.Is env="prod">
2832
<Infra.Aws.Tags tags={{ ENV: "production" }} />
@@ -91,7 +95,7 @@ export const Extensions = () => {
9195

9296
{/* Security 👇 */}
9397
<Api.Extension src={"/extensions/MyApiKey.ts"} />
94-
{/*<Security.ApiKey.AfterUpdate src={"/extensions/MyApiKeyAfterUpdate.ts"} />*/}
98+
<Security.ApiKey.AfterUpdate src={"/extensions/MyApiKeyAfterUpdate.ts"} />
9599

96100
{/* 🚧 WIP 👇 */}
97101
{/*<Security.ApiKeyBeforeCreate src={"/extensions/ApiKeyBeforeCreate.ts"} />*/}

0 commit comments

Comments
 (0)