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

Stripe construct #87

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/constructs/abstracts/StripeConstruct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { StripeProvider } from "@lift/providers";
import type { ConstructInterface } from "@lift/constructs";

export abstract class StripeConstruct<T> implements ConstructInterface {
static create<C extends StripeConstruct = StripeConstruct>(
this: {
new (provider: StripeProvider, id: string, configuration: Record<string, unknown>): C;
},
provider: StripeProvider,
id: string,
configuration: Record<string, unknown>
): C {
/**
* We are passing a `configuration` of type `Record<string, unknown>` to a parameter
* of stricter type. This is theoretically invalid.
*
* In practice however, `configuration` has been validated with the exact JSON schema
* of the construct. And that construct has generated the type for `configuration` based
* on that schema.
* As such, we _know_ that `configuration` has the correct type, it is just not validated
* by TypeScript's compiler.
*/
return new this(provider, id, configuration);
}

abstract outputs?(): Record<string, () => Promise<string | undefined>>;

protected abstract add(configuration: Record<string, unknown>): void | Promise<void>;
protected abstract update(resources: T, configuration: Record<string, unknown>): void | Promise<void>;
protected abstract destroy(resources: T): void | Promise<void>;
}
1 change: 1 addition & 0 deletions src/constructs/abstracts/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { AwsConstruct } from "./AwsConstruct";
export { StripeConstruct } from "./StripeConstruct";
1 change: 0 additions & 1 deletion src/constructs/aws/Queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import type { CliOptions } from "../../types/serverless";
const QUEUE_DEFINITION = {
type: "object",
properties: {
type: { const: "queue" },
worker: {
type: "object",
properties: {
Expand Down
1 change: 0 additions & 1 deletion src/constructs/aws/StaticWebsite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import ServerlessError from "../../utils/error";
const STATIC_WEBSITE_DEFINITION = {
type: "object",
properties: {
type: { const: "static-website" },
path: { type: "string" },
domain: {
anyOf: [
Expand Down
2 changes: 0 additions & 2 deletions src/constructs/aws/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { PolicyStatement } from "../../CloudFormation";
const STORAGE_DEFINITION = {
type: "object",
properties: {
type: { const: "storage" },
archive: { type: "number", minimum: 30 },
encryption: {
anyOf: [{ const: "s3" }, { const: "kms" }],
Expand All @@ -18,7 +17,6 @@ const STORAGE_DEFINITION = {
additionalProperties: false,
} as const;
const STORAGE_DEFAULTS: Required<FromSchema<typeof STORAGE_DEFINITION>> = {
type: "storage",
archive: 45,
encryption: "s3",
};
Expand Down
4 changes: 1 addition & 3 deletions src/constructs/aws/Vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import type { ConstructInterface } from "@lift/constructs";

const VPC_DEFINITION = {
type: "object",
properties: {
type: { const: "vpc" },
},
properties: {},
additionalProperties: false,
required: [],
} as const;
Expand Down
1 change: 0 additions & 1 deletion src/constructs/aws/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ServerlessError from "../../utils/error";
const WEBHOOK_DEFINITION = {
type: "object",
properties: {
type: { const: "webhook" },
authorizer: {
type: "object",
properties: {
Expand Down
76 changes: 76 additions & 0 deletions src/constructs/stripe/Webhook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { FromSchema } from "json-schema-to-ts";
import type { Stripe } from "stripe";
import type { StripeProvider } from "@lift/providers";
import { StripeConstruct } from "@lift/constructs/abstracts";

const WEBHOOK_DEFINITION = {
type: "object",
properties: {
url: { type: "string" },
enabledEvents: { type: "array", items: { type: "string" } },
},
required: ["url", "enabledEvents"],
additionalProperties: false,
} as const;

type Configuration = FromSchema<typeof WEBHOOK_DEFINITION>;

export class Webhook extends StripeConstruct {
public static type = "webhook";
public static schema = WEBHOOK_DEFINITION;

private readonly sdk: Stripe;

constructor(
private readonly provider: StripeProvider,
private readonly id: string,
private readonly configuration: Configuration
) {
super();
this.sdk = provider.sdk;
const resolvedConfiguration = Object.assign({}, configuration);
}

protected async add(configuration: Configuration): Promise<void> {
if (configuration.enabledEvents.every(this.validateEvent)) {
const webhook = await this.sdk.webhookEndpoints.create({
url: configuration.url,
enabled_events: configuration.enabledEvents,
});
this.provider.referenceNewStripeResources(this.id, webhook.id);
}
}

protected async update(resources: string, configuration: Configuration): Promise<void> {
const webhookId = this.provider.getStripeResources(this.id);
if (typeof webhookId !== "string") {
throw new Error("This should not happen");
}
if (configuration.enabledEvents.every(this.validateEvent)) {
await this.sdk.webhookEndpoints.update(webhookId, {
url: configuration.url,
enabled_events: configuration.enabledEvents,
});
}
}

protected async destroy(resources: string): Promise<void> {
const webhookId = this.provider.getStripeResources(this.id);
if (typeof webhookId !== "string") {
throw new Error("This should not happen");
}
await this.sdk.webhookEndpoints.del(webhookId);
}

private validateEvent(event: string): event is Stripe.WebhookEndpointCreateParams.EnabledEvent {
return true;
}

outputs(): Record<string, () => Promise<string | undefined>> {
return {};
}

variables(): Record<string, unknown> {
return {};
}
}
1 change: 1 addition & 0 deletions src/constructs/stripe/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Webhook } from "./Webhook";
4 changes: 3 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class LiftPlugin {
this.constructsSchema.patternProperties[CONSTRUCT_ID_PATTERN].allOf as unknown as Record<string, unknown>[]
).push({
oneOf: this.getAllConstructClasses().map((Construct) => {
return this.defineSchemaWithType(Construct.type, Construct.schema);
const constructSchema = this.defineSchemaWithType(Construct.type, Construct.schema);

return merge(constructSchema, { properties: { provider: { type: "string" } } });
}),
});
}
Expand Down
16 changes: 16 additions & 0 deletions src/providers/StripeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { get, has } from "lodash";
import { Stripe } from "stripe";
import type { ConstructInterface, StaticConstructInterface } from "@lift/constructs";
import type { ProviderInterface } from "@lift/providers";
import { Webhook } from "@lift/constructs/stripe";
import type { FromSchema } from "json-schema-to-ts";
import type { Serverless } from "../types/serverless";
import ServerlessError from "../utils/error";
Expand Down Expand Up @@ -61,9 +62,22 @@ export class StripeProvider implements ProviderInterface {

private config: { apiKey: string; accountId?: string };
public sdk: Stripe;
private state: Record<string, unknown>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need state management here? I'm wondering if this is a bit early to add.

constructor(private readonly serverless: Serverless, private readonly id: string, profile?: string) {
this.config = this.resolveConfiguration(profile);
this.sdk = new Stripe(this.config.apiKey, { apiVersion: "2020-08-27" });
this.state = {};
}

public referenceNewStripeResources<T>(constructId: string, resources: T): void {
if (this.state[constructId] !== undefined) {
throw new ServerlessError("State information were already existing for this construct", "");
}
this.state[constructId] = resources;
}

public getStripeResources(constructId: string): unknown {
return this.state[constructId];
}

createConstruct(type: string, id: string): ConstructInterface {
Expand Down Expand Up @@ -135,3 +149,5 @@ export class StripeProvider implements ProviderInterface {
};
}
}

StripeProvider.registerConstructs(Webhook);
8 changes: 8 additions & 0 deletions test/fixtures/stripe/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ providers:
profile: myProfile
stripeProviderWithoutProfile:
type: stripe

constructs:
monWebhook:
type: webhook
provider: stripeProviderWithProfile
url: test
enabledEvents:
- payin