Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSTConstruct: Add optional resource type to function binding #3529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Api.ts
Expand Up @@ -996,6 +996,7 @@ export class Api<
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "api",
resourceType: 'Api',
variables: {
url: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/ApiGatewayV1Api.ts
Expand Up @@ -797,6 +797,7 @@ export class ApiGatewayV1Api<
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "api",
resourceType: 'ApiGatewayV1Api',
variables: {
url: {
type: "plain",
Expand Down
7 changes: 4 additions & 3 deletions packages/sst/src/constructs/App.ts
Expand Up @@ -5,6 +5,7 @@ import { Stack } from "./Stack.js";
import {
SSTConstruct,
SSTConstructMetadata,
SST_CONSTRUCT_TYPE,
isSSTConstruct,
isStackConstruct,
} from "./Construct.js";
Expand Down Expand Up @@ -293,7 +294,7 @@ export class App extends CDKApp {
return;
}

const className = c.constructor.name;
const clientType = binding.clientType || c.constructor.name;
const id = c.id;

fs.appendFileSync(
Expand All @@ -303,7 +304,7 @@ export class App extends CDKApp {
[
`import "sst/node/${binding.clientPackage}";`,
`declare module "sst/node/${binding.clientPackage}" {`,
` export interface ${className}Resources {`,
` export interface ${clientType}Resources {`,
` "${id}": string;`,
` }`,
`}`,
Expand All @@ -313,7 +314,7 @@ export class App extends CDKApp {
: [
`import "sst/node/${binding.clientPackage}";`,
`declare module "sst/node/${binding.clientPackage}" {`,
` export interface ${className}Resources {`,
` export interface ${clientType}Resources {`,
` "${id}": {`,
...binding.variables.map((p) => ` ${p}: string;`),
` }`,
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/AppSyncApi.ts
Expand Up @@ -754,6 +754,7 @@ export class AppSyncApi extends Construct implements SSTConstruct {

return {
clientPackage: "api",
resourceType: "AppSyncApi",
variables: {
url: {
type: "plain",
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/constructs/AstroSite.ts
Expand Up @@ -253,4 +253,8 @@ export class AstroSite extends SsrSite {
...this.getConstructMetadataBase(),
};
}

public getBaseFunctionBindings() {
return this.createFunctionBinding('AstroSite');
}
}
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Auth.ts
Expand Up @@ -150,6 +150,7 @@ export class Auth extends Construct implements SSTConstruct {
const app = this.node.root as App;
return {
clientPackage: "auth",
resourceType: "Auth",
variables: {
publicKey: {
type: "secret",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Bucket.ts
Expand Up @@ -480,6 +480,7 @@ export class Bucket extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "bucket",
resourceType: "Bucket",
variables: {
bucketName: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Construct.ts
Expand Up @@ -16,6 +16,7 @@ export interface SSTConstructMetadata<
local?: L;
}


export interface SSTConstruct extends Construct {
id: string;
getConstructMetadata(): SSTConstructMetadata;
Expand Down
2 changes: 1 addition & 1 deletion packages/sst/src/constructs/Cron.ts
Expand Up @@ -2,7 +2,7 @@ import { Construct } from "constructs";
import * as events from "aws-cdk-lib/aws-events";
import * as eventsTargets from "aws-cdk-lib/aws-events-targets";

import { getFunctionRef, SSTConstruct } from "./Construct.js";
import { getFunctionRef, SST_CONSTRUCT_TYPE, SSTConstruct } from "./Construct.js";
import { App } from "./App.js";
import {
Function as Func,
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/EventBus.ts
Expand Up @@ -584,6 +584,7 @@ export class EventBus extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "event-bus",
resourceType: "EventBus",
variables: {
eventBusName: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Function.ts
Expand Up @@ -1133,6 +1133,7 @@ export class Function extends CDKFunction implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "function",
resourceType: "Function",
variables: {
functionName: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Job.ts
Expand Up @@ -380,6 +380,7 @@ export class Job extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "job",
resourceType: "Job",
variables: {
functionName: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/KinesisStream.ts
Expand Up @@ -315,6 +315,7 @@ export class KinesisStream extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "kinesis-stream",
resourceType: "KinesisStream",
variables: {
streamName: {
type: "plain",
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/constructs/NextjsSite.ts
Expand Up @@ -525,6 +525,10 @@ export class NextjsSite extends SsrSite {
};
}

public getFunctionBinding() {
return this.createFunctionBinding('NextjsSite');
}

private wrapServerFunction(config: SsrFunctionProps | EdgeFunctionProps) {
const { path: sitePath, experimental, cdk } = this.props;
const stack = Stack.of(this);
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Parameter.ts
Expand Up @@ -52,6 +52,7 @@ export class Parameter extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "config",
resourceType: "Parameter",
variables: {
value: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Queue.ts
Expand Up @@ -310,6 +310,7 @@ export class Queue extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "queue",
resourceType: "Queue",
variables: {
queueUrl: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/RDS.ts
Expand Up @@ -316,6 +316,7 @@ export class RDS extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "rds",
resourceType: "RDS",
variables: {
clusterArn: {
type: "plain",
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/constructs/RemixSite.ts
Expand Up @@ -251,4 +251,8 @@ export class RemixSite extends SsrSite {
...this.getConstructMetadataBase(),
};
}

public getFunctionBinding() {
return this.createFunctionBinding('RemixSite');
}
}
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Secret.ts
Expand Up @@ -49,6 +49,7 @@ export class Secret extends Construct implements SSTConstruct {
const partition = Stack.of(this).partition;
return {
clientPackage: "config",
resourceType: "Secret",
variables: {
value: {
type: "secret",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Service.ts
Expand Up @@ -723,6 +723,7 @@ export class Service extends Construct implements SSTConstruct {
return this.distribution
? {
clientPackage: "service",
resourceType: "Service",
variables: {
url: this.doNotDeploy
? {
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/constructs/SolidStartSite.ts
Expand Up @@ -116,4 +116,8 @@ export class SolidStartSite extends SsrSite {
...this.getConstructMetadataBase(),
};
}

public getFunctionBinding() {
return this.createFunctionBinding('SolidStartSite');
}
}
5 changes: 4 additions & 1 deletion packages/sst/src/constructs/SsrSite.ts
Expand Up @@ -1492,10 +1492,13 @@ function handler(event) {
>;

/** @internal */
public getFunctionBinding(): FunctionBindingProps {
public abstract getFunctionBinding(): FunctionBindingProps;

protected createFunctionBinding(resourceType: string): FunctionBindingProps {
const app = this.node.root as App;
return {
clientPackage: "site",
resourceType,
variables: {
url: this.doNotDeploy
? {
Expand Down
3 changes: 2 additions & 1 deletion packages/sst/src/constructs/StaticSite.ts
Expand Up @@ -456,6 +456,7 @@ export class StaticSite extends Construct implements SSTConstruct {
const app = this.node.root as App;
return {
clientPackage: "site",
resourceType: "StaticSite",
variables: {
url: this.doNotDeploy
? {
Expand Down Expand Up @@ -798,7 +799,7 @@ interface ImportMeta {
function handler(event) {
var request = event.request;
var uri = request.uri;

if (uri.startsWith("/.well-known/")) {
return request;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sst/src/constructs/SvelteKitSite.ts
Expand Up @@ -162,4 +162,8 @@ export class SvelteKitSite extends SsrSite {
...this.getConstructMetadataBase(),
};
}

public getFunctionBinding() {
return this.createFunctionBinding('SvelteKitSite');
}
}
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Table.ts
Expand Up @@ -615,6 +615,7 @@ export class Table extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "table",
resourceType: "Table",
variables: {
tableName: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/Topic.ts
Expand Up @@ -386,6 +386,7 @@ export class Topic extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "topic",
resourceType: "Topic",
variables: {
topicArn: {
type: "plain",
Expand Down
1 change: 1 addition & 0 deletions packages/sst/src/constructs/WebSocketApi.ts
Expand Up @@ -479,6 +479,7 @@ export class WebSocketApi extends Construct implements SSTConstruct {
public getFunctionBinding(): FunctionBindingProps {
return {
clientPackage: "websocket-api",
resourceType: "WebSocketApi",
variables: {
url: {
type: "plain",
Expand Down
2 changes: 2 additions & 0 deletions packages/sst/src/constructs/util/functionBinding.ts
Expand Up @@ -6,6 +6,7 @@ import { Config } from "../../config.js";

export interface FunctionBindingProps {
clientPackage: string;
resourceType?: string;
permissions: Record<string, string[]>;
variables: Record<
string,
Expand Down Expand Up @@ -105,6 +106,7 @@ export function bindType(c: SSTConstruct) {

return {
clientPackage: binding.clientPackage,
clientType: binding.resourceType || undefined,
variables: Object.keys(binding.variables),
};
}
Expand Down