@aws-blocks/create-block 0.2.0

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.
@@ -0,0 +1,47 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { test } from 'node:test';
5
+ import assert from 'node:assert';
6
+ import * as cdk from 'aws-cdk-lib';
7
+ import type { Construct } from 'constructs';
8
+ import { Template } from 'aws-cdk-lib/assertions';
9
+ import { Scope } from '@aws-blocks/core/cdk';
10
+ import { __BB_CLASS__ } from './index.cdk.js';
11
+
12
+ // Minimal BlocksStack-shaped parent so __BB_CLASS__ can grant IAM on this.handler
13
+ // and synth into a real stack. Mirrors the shape a real BlocksStack exposes.
14
+ class StubBlocksStack extends cdk.Stack {
15
+ public readonly handler: cdk.aws_lambda.Function;
16
+ public readonly id: string;
17
+ constructor(scope: Construct, id: string) {
18
+ super(scope, id);
19
+ this.id = id;
20
+ (globalThis as { CURRENT_BLOCKS_STACK?: unknown }).CURRENT_BLOCKS_STACK = this;
21
+ this.handler = new cdk.aws_lambda.Function(this, 'StubHandler', {
22
+ runtime: cdk.aws_lambda.Runtime.NODEJS_22_X,
23
+ handler: 'index.handler',
24
+ code: cdk.aws_lambda.Code.fromInline('exports.handler = async () => {};'),
25
+ });
26
+ }
27
+ }
28
+
29
+ function setup(): { stack: StubBlocksStack; parent: Scope } {
30
+ const app = new cdk.App();
31
+ const stack = new StubBlocksStack(app, 'TestStack');
32
+ return { stack, parent: new Scope('app') };
33
+ }
34
+
35
+ test('CDK: the construct synthesizes within a stack', () => {
36
+ const { stack, parent } = setup();
37
+ new __BB_CLASS__(parent, 'thing');
38
+ // TODO: once you provision infra, assert it here, e.g.:
39
+ // Template.fromStack(stack).resourceCountIs('AWS::DynamoDB::Table', 1);
40
+ assert.doesNotThrow(() => Template.fromStack(stack));
41
+ });
42
+
43
+ test('CDK: runtime methods throw during synth (synthGuard)', () => {
44
+ const { parent } = setup();
45
+ const bb = new __BB_CLASS__(parent, 'thing');
46
+ assert.throws(() => (bb as unknown as { echo: (s: string) => never }).echo('x'), /synth/i);
47
+ });
@@ -0,0 +1,34 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Scope, synthGuard } from '@aws-blocks/core/cdk';
5
+ import type { ScopeParent } from '@aws-blocks/core';
6
+ import type { __BB_CLASS__Options } from './types.js';
7
+
8
+ // ── Public types + errors ────────────────────────────────────────────────────
9
+ export { __BB_CLASS__Errors } from './errors.js';
10
+ export type { __BB_CLASS__Options } from './types.js';
11
+
12
+ /**
13
+ * The `cdk` export: runs during `cdk synth` and provisions this block's
14
+ * infrastructure. It extends `Scope` (never wraps it) so infra discovery and
15
+ * IAM propagation work.
16
+ */
17
+ export class __BB_CLASS__ extends Scope {
18
+ constructor(scope: ScopeParent, id: string, _options?: __BB_CLASS__Options) {
19
+ super(id, { parent: scope });
20
+ // TODO: provision your infrastructure and grant the shared Lambda access.
21
+ // Name resources off `this.fullId` so the runtime can derive the same name.
22
+ // Example:
23
+ // const table = new Table(this, 'table', { tableName: this.fullId.substring(0, 255), ... });
24
+ // table.grantReadWriteData(this.handler);
25
+ // registerConfig(this, 'BLOCKS_THING_FLAG', '...'); // extra (non-name) config only
26
+ }
27
+
28
+ // Runtime methods are not available during CDK synth (AGENTS.md rule 4). Stub
29
+ // every method your runtime exposes so a top-level call fails with an
30
+ // actionable message instead of a cryptic "X is not a function".
31
+ echo(..._args: unknown[]): never {
32
+ return synthGuard('__BB_CLASS__', 'echo');
33
+ }
34
+ }
@@ -0,0 +1,46 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { Scope, registerSdkIdentifiers } from '@aws-blocks/core';
5
+ import type { ScopeParent } from '@aws-blocks/core';
6
+ import { BB_NAME, BB_VERSION } from './version.js';
7
+
8
+ // ── Public types + errors ────────────────────────────────────────────────────
9
+ export { __BB_CLASS__Errors } from './errors.js';
10
+ export type { __BB_CLASS__Options } from './types.js';
11
+
12
+ import type { __BB_CLASS__Options } from './types.js';
13
+
14
+ /**
15
+ * TODO: one-line summary of what __BB_CLASS__ does.
16
+ *
17
+ * This is the **local-dev / test** implementation (the `default` + `types`
18
+ * export). It runs during `npm run dev` and unit tests, so implement it with
19
+ * in-memory or on-disk state — never a real AWS call. Keep its public surface
20
+ * identical to `index.aws.ts` (the deployed runtime). See `bb-kv-store` for a
21
+ * worked example; this skeleton is intentionally storage-agnostic.
22
+ *
23
+ * **When to use:** TODO.
24
+ *
25
+ * **When NOT to use:** TODO.
26
+ */
27
+ export class __BB_CLASS__ extends Scope {
28
+ constructor(scope: ScopeParent, id: string, _options?: __BB_CLASS__Options) {
29
+ super(id, { parent: scope, bbName: BB_NAME, bbVersion: BB_VERSION });
30
+ // The CDK layer provisions your resource under this same `fullId`. Register
31
+ // its identifier(s) here (e.g. `{ tableName: \`mock-${this.fullId}\` }`) so
32
+ // methods can resolve them with `getSdkIdentifiers(this)` at call time.
33
+ registerSdkIdentifiers(this.fullId, {});
34
+ }
35
+
36
+ /**
37
+ * TODO: replace with your block's real API. This example method exists only
38
+ * so the generated package builds and tests green out of the box.
39
+ *
40
+ * @param input - TODO describe.
41
+ * @returns TODO describe.
42
+ */
43
+ async echo(input: string): Promise<string> {
44
+ return input;
45
+ }
46
+ }
@@ -0,0 +1,29 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { test, beforeEach } from 'node:test';
5
+ import assert from 'node:assert';
6
+ import { rmSync } from 'node:fs';
7
+ import { Scope } from '@aws-blocks/core';
8
+ import { __BB_CLASS__, __BB_CLASS__Errors } from './index.mock.js';
9
+
10
+ // A real, typed parent Scope — no casts (this test represents customer usage).
11
+ const parent = new Scope('test-app');
12
+ let n = 0;
13
+ const make = () => new __BB_CLASS__(parent, `thing-${n++}`);
14
+
15
+ // Reset mock data between tests (blocks that persist to disk write under .bb-data).
16
+ beforeEach(() => {
17
+ try {
18
+ rmSync('.bb-data', { recursive: true, force: true });
19
+ } catch {}
20
+ });
21
+
22
+ test('echo returns its input (TODO: replace with real coverage)', async () => {
23
+ const bb = make();
24
+ assert.strictEqual(await bb.echo('hello'), 'hello');
25
+ });
26
+
27
+ test('error constants are defined', () => {
28
+ assert.ok(__BB_CLASS__Errors.InvalidInput);
29
+ });
@@ -0,0 +1,16 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /** Ensures the four entry points expose a consistent public error surface. */
5
+ import { test } from 'node:test';
6
+ import assert from 'node:assert/strict';
7
+ import { __BB_CLASS__Errors as MockErrors } from './index.mock.js';
8
+ import { __BB_CLASS__Errors as AwsErrors } from './index.aws.js';
9
+ import { __BB_CLASS__Errors as CdkErrors } from './index.cdk.js';
10
+ import { __BB_CLASS__Errors as BrowserErrors } from './index.browser.js';
11
+
12
+ test('error constants are identical across mock / aws / cdk / browser', () => {
13
+ assert.deepEqual(MockErrors, AwsErrors);
14
+ assert.deepEqual(MockErrors, CdkErrors);
15
+ assert.deepEqual(MockErrors, BrowserErrors);
16
+ });
@@ -0,0 +1,13 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * Shared types for __BB_CLASS__. Imported by the mock, aws, cdk, and browser
6
+ * entry points. Types only — this file has zero runtime dependencies.
7
+ */
8
+
9
+ /** Options for constructing a {@link __BB_CLASS__}. */
10
+ export interface __BB_CLASS__Options {
11
+ /** TODO: add this block's configuration options (all optional — see AGENTS.md). */
12
+ label?: string;
13
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "references": [
9
+ { "path": "../core" }
10
+ ]
11
+ }