-
Notifications
You must be signed in to change notification settings - Fork 115
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
fredericbarthelet
wants to merge
3
commits into
master
Choose a base branch
from
stripe-construct
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Stripe construct #87
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { AwsConstruct } from "./AwsConstruct"; | ||
export { StripeConstruct } from "./StripeConstruct"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Webhook } from "./Webhook"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.