nomoss 0.0.1
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.
- package/LICENSE +19 -0
- package/README.md +45 -0
- package/bin/nomoss +13 -0
- package/docs/code-documentation.md +36 -0
- package/docs/effect-native-development.md +55 -0
- package/docs/engine-design.md +93 -0
- package/docs/linting.md +88 -0
- package/docs/releasing.md +13 -0
- package/docs/resource-command-policy-stage.md +71 -0
- package/docs/resource-graph-mvp.md +31 -0
- package/docs/resource-graph-testing-approach.md +11 -0
- package/docs/resource-lifecycle-tracker.md +93 -0
- package/docs/testing.md +86 -0
- package/examples/upload-events/README.md +41 -0
- package/media/nomoss.gif +0 -0
- package/package.json +64 -0
- package/src/cli.ts +18 -0
- package/src/cliCommand.ts +186 -0
- package/src/cliRuntimeLayer.ts +49 -0
- package/src/core/index.ts +17 -0
- package/src/core/lifecycle.ts +127 -0
- package/src/core/model.ts +229 -0
- package/src/core/physicalNameStore.ts +226 -0
- package/src/core/planner.ts +645 -0
- package/src/core/resourceCommandPolicy.ts +79 -0
- package/src/core/resourceGraphBuilder.ts +167 -0
- package/src/core/resourceGraphStore.ts +288 -0
- package/src/core/resourceOutputResolver.ts +185 -0
- package/src/core/resourcePolicy.ts +77 -0
- package/src/core/resourceStackDefinition.ts +17 -0
- package/src/core/resourceStackLifecycle.ts +456 -0
- package/src/core/resourceStackOperations.ts +129 -0
- package/src/core/runtimeLayer.ts +73 -0
- package/src/core/stackWorkflowRenderer.ts +239 -0
- package/src/core/stateSecretService.ts +553 -0
- package/src/core/stateStore.ts +652 -0
- package/src/core/tracing.ts +26 -0
- package/src/index.ts +2 -0
- package/src/providers/aws/awsApply.ts +132 -0
- package/src/providers/aws/awsBucket.ts +329 -0
- package/src/providers/aws/awsBucketNotification.ts +351 -0
- package/src/providers/aws/awsBucketNotificationResourcePolicy.ts +251 -0
- package/src/providers/aws/awsBucketResourcePolicy.ts +198 -0
- package/src/providers/aws/awsProviderLayer.ts +274 -0
- package/src/providers/aws/awsQueue.ts +346 -0
- package/src/providers/aws/awsQueuePolicy.ts +419 -0
- package/src/providers/aws/awsQueuePolicyResourcePolicy.ts +215 -0
- package/src/providers/aws/awsQueueResourcePolicy.ts +270 -0
- package/src/providers/aws/awsReconciliation.ts +92 -0
- package/src/providers/aws/awsRefresh.ts +50 -0
- package/src/providers/aws/awsResourceLifecycle.ts +118 -0
- package/src/providers/aws/awsResourcePolicy.ts +83 -0
- package/src/providers/aws/awsResources.ts +274 -0
- package/src/providers/aws/awsRuntimeLayer.ts +21 -0
- package/src/providers/aws/awsSqsTransport.ts +33 -0
- package/src/providers/aws/awsStackLifecycle.ts +272 -0
- package/src/providers/aws/awsTagging.ts +186 -0
- package/src/providers/aws/constants.ts +3 -0
- package/src/providers/aws/index.ts +22 -0
- package/src/providers/aws/sampleStack.ts +65 -0
- package/src/providers/aws/stackWorkflow.ts +443 -0
- package/src/providers/index.ts +5 -0
- package/src/providers/provider.ts +5 -0
- package/src/providers/providerRuntimeLayer.ts +121 -0
- package/src/providers/resourceCommandPolicyLayer.ts +240 -0
- package/src/providers/stripe/index.ts +15 -0
- package/src/providers/stripe/stripeAccount.ts +139 -0
- package/src/providers/stripe/stripeBillingConfigurationExport.ts +180 -0
- package/src/providers/stripe/stripeBillingConfigurationExportResourcePolicy.ts +178 -0
- package/src/providers/stripe/stripeBillingPortalConfiguration.ts +153 -0
- package/src/providers/stripe/stripeBillingPortalConfigurationResourcePolicy.ts +177 -0
- package/src/providers/stripe/stripeCustomer.ts +148 -0
- package/src/providers/stripe/stripeCustomerResourcePolicy.ts +160 -0
- package/src/providers/stripe/stripePrice.ts +218 -0
- package/src/providers/stripe/stripePriceResourcePolicy.ts +151 -0
- package/src/providers/stripe/stripeProduct.ts +171 -0
- package/src/providers/stripe/stripeProductResourcePolicy.ts +161 -0
- package/src/providers/stripe/stripeResources.ts +454 -0
- package/src/providers/stripe/stripeRuntimeLayer.ts +131 -0
- package/src/providers/stripe/stripeWebhookEndpoint.ts +293 -0
- package/src/providers/stripe/stripeWebhookEndpointResourcePolicy.ts +198 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Context, Effect, Match, Struct } from "effect";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type PlanDecision,
|
|
5
|
+
ResourceCommand,
|
|
6
|
+
ResourceCommandResult,
|
|
7
|
+
ResourceCommandUnsupported,
|
|
8
|
+
} from "../../core/lifecycle.js";
|
|
9
|
+
import {
|
|
10
|
+
ResourceModel,
|
|
11
|
+
type ResourceNode,
|
|
12
|
+
readResourceSchemaAnnotation,
|
|
13
|
+
} from "../../core/model.js";
|
|
14
|
+
import {
|
|
15
|
+
StripeCustomerLifecycle,
|
|
16
|
+
type StripeCustomerOutputs,
|
|
17
|
+
StripeCustomerOutputsSchema,
|
|
18
|
+
StripeCustomerPropsSchema,
|
|
19
|
+
StripeCustomerUpdatePropsSchema,
|
|
20
|
+
stripeCustomerOutputsFromState,
|
|
21
|
+
} from "./stripeCustomer.js";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Stripe Customer commands run through the shared resource command protocol
|
|
25
|
+
* so stack execution can mix providers without app-side branching.
|
|
26
|
+
*/
|
|
27
|
+
export class StripeCustomerResourcePolicy extends Context.Service<StripeCustomerResourcePolicy>()(
|
|
28
|
+
"nomoss/providers/stripe/stripeCustomerResourcePolicy",
|
|
29
|
+
{
|
|
30
|
+
make: Effect.gen(function* () {
|
|
31
|
+
const lifecycle = yield* StripeCustomerLifecycle;
|
|
32
|
+
const model = yield* ResourceModel;
|
|
33
|
+
const schema = yield* readResourceSchemaAnnotation(
|
|
34
|
+
StripeCustomerPropsSchema,
|
|
35
|
+
).pipe(Effect.fromOption);
|
|
36
|
+
|
|
37
|
+
const resourceNode = Effect.fn(
|
|
38
|
+
"StripeCustomerResourcePolicy.resourceNode",
|
|
39
|
+
)(function* (node: ResourceNode, outputs: StripeCustomerOutputs) {
|
|
40
|
+
const props = yield* model.decodeProps(node, StripeCustomerPropsSchema);
|
|
41
|
+
const appliedNode = yield* model.nodeFromResource({
|
|
42
|
+
key: node.key,
|
|
43
|
+
propsSchema: StripeCustomerPropsSchema,
|
|
44
|
+
outputsSchema: StripeCustomerOutputsSchema,
|
|
45
|
+
props,
|
|
46
|
+
outputs,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return appliedNode;
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const create = Effect.fn("StripeCustomerResourcePolicy.create")(
|
|
53
|
+
function* (node: ResourceNode) {
|
|
54
|
+
const props = yield* model.decodeProps(
|
|
55
|
+
node,
|
|
56
|
+
StripeCustomerPropsSchema,
|
|
57
|
+
);
|
|
58
|
+
const customer = yield* lifecycle.createCustomer(props);
|
|
59
|
+
const outputs = stripeCustomerOutputsFromState(customer);
|
|
60
|
+
const appliedNode = yield* resourceNode(node, outputs);
|
|
61
|
+
|
|
62
|
+
return ResourceCommandResult.Created({ node: appliedNode });
|
|
63
|
+
},
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const update = Effect.fn("StripeCustomerResourcePolicy.update")(
|
|
67
|
+
function* (node: ResourceNode, current: ResourceNode) {
|
|
68
|
+
const props = yield* model.decodeProps(
|
|
69
|
+
node,
|
|
70
|
+
StripeCustomerPropsSchema,
|
|
71
|
+
);
|
|
72
|
+
const currentOutputs = yield* model.decodeOutputs(
|
|
73
|
+
current,
|
|
74
|
+
StripeCustomerOutputsSchema,
|
|
75
|
+
);
|
|
76
|
+
const input = yield* StripeCustomerUpdatePropsSchema.makeEffect(
|
|
77
|
+
Struct.assign(props, {
|
|
78
|
+
customer: currentOutputs.CustomerId,
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
const customer = yield* lifecycle.updateCustomer(input);
|
|
82
|
+
const outputs = stripeCustomerOutputsFromState(customer);
|
|
83
|
+
const appliedNode = yield* resourceNode(node, outputs);
|
|
84
|
+
|
|
85
|
+
return ResourceCommandResult.Updated({ node: appliedNode });
|
|
86
|
+
},
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const destroy = Effect.fn("StripeCustomerResourcePolicy.destroy")(
|
|
90
|
+
function* (node: ResourceNode) {
|
|
91
|
+
const outputs = yield* model.decodeOutputs(
|
|
92
|
+
node,
|
|
93
|
+
StripeCustomerOutputsSchema,
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
yield* lifecycle.deleteCustomer(outputs.CustomerId);
|
|
97
|
+
|
|
98
|
+
return ResourceCommandResult.Destroyed({ node });
|
|
99
|
+
},
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const apply = Effect.fn("StripeCustomerResourcePolicy.apply")(function* (
|
|
103
|
+
decision: PlanDecision,
|
|
104
|
+
) {
|
|
105
|
+
return yield* Match.value(decision).pipe(
|
|
106
|
+
Match.tagsExhaustive({
|
|
107
|
+
Create: ({ node }) => create(node),
|
|
108
|
+
Update: ({ node, current }) => update(node, current),
|
|
109
|
+
Destroy: ({ node }) => destroy(node),
|
|
110
|
+
NoOp: () =>
|
|
111
|
+
Effect.fail(
|
|
112
|
+
new ResourceCommandUnsupported({
|
|
113
|
+
command: ResourceCommand.Apply({ decision }),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
Repair: () =>
|
|
117
|
+
Effect.fail(
|
|
118
|
+
new ResourceCommandUnsupported({
|
|
119
|
+
command: ResourceCommand.Apply({ decision }),
|
|
120
|
+
}),
|
|
121
|
+
),
|
|
122
|
+
Delete: () =>
|
|
123
|
+
Effect.fail(
|
|
124
|
+
new ResourceCommandUnsupported({
|
|
125
|
+
command: ResourceCommand.Apply({ decision }),
|
|
126
|
+
}),
|
|
127
|
+
),
|
|
128
|
+
}),
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
schema,
|
|
134
|
+
create,
|
|
135
|
+
update,
|
|
136
|
+
destroy,
|
|
137
|
+
apply,
|
|
138
|
+
execute: Effect.fn("StripeCustomerResourcePolicy.execute")(function* (
|
|
139
|
+
command: ResourceCommand,
|
|
140
|
+
) {
|
|
141
|
+
return yield* Match.value(command).pipe(
|
|
142
|
+
Match.tagsExhaustive({
|
|
143
|
+
Read: () =>
|
|
144
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
145
|
+
Diff: () =>
|
|
146
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
147
|
+
Apply: ({ decision }) => apply(decision),
|
|
148
|
+
Create: ({ node }) => create(node),
|
|
149
|
+
Update: () =>
|
|
150
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
151
|
+
Delete: () =>
|
|
152
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
153
|
+
Destroy: ({ node }) => destroy(node),
|
|
154
|
+
}),
|
|
155
|
+
);
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
}),
|
|
159
|
+
},
|
|
160
|
+
) {}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { Credentials } from "@distilled.cloud/stripe";
|
|
2
|
+
import type {
|
|
3
|
+
GetPricesPriceInput,
|
|
4
|
+
PostPricesPriceInput,
|
|
5
|
+
} from "@distilled.cloud/stripe/Operations";
|
|
6
|
+
import {
|
|
7
|
+
GetPricesPrice,
|
|
8
|
+
PostPrices,
|
|
9
|
+
PostPricesInput,
|
|
10
|
+
PostPricesOutput,
|
|
11
|
+
PostPricesPrice,
|
|
12
|
+
} from "@distilled.cloud/stripe/Operations";
|
|
13
|
+
import { Context, Effect, Match, Option, Schema } from "effect";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
annotateResourceSchema,
|
|
17
|
+
type ResourceOutputRef,
|
|
18
|
+
ResourceOutputRefSchema,
|
|
19
|
+
} from "../../core/model.js";
|
|
20
|
+
import { StripeApiVersion } from "./stripeAccount.js";
|
|
21
|
+
|
|
22
|
+
export const StripePricePropsSchema = annotateResourceSchema(PostPricesInput, {
|
|
23
|
+
provider: "stripe",
|
|
24
|
+
service: "billing",
|
|
25
|
+
resource: "price",
|
|
26
|
+
operation: "create",
|
|
27
|
+
stateSecretOutputKeys: [],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export type StripePriceProps = Schema.Schema.Type<
|
|
31
|
+
typeof StripePricePropsSchema
|
|
32
|
+
>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A Price declaration can point at a Product resource ref before Stripe creates
|
|
36
|
+
* the Product id. Nomoss resolves the ref during apply and then calls the
|
|
37
|
+
* generated Stripe Price operation with a concrete Product id.
|
|
38
|
+
*/
|
|
39
|
+
export const StripePriceDeclarationPropsSchema = annotateResourceSchema(
|
|
40
|
+
Schema.Struct({
|
|
41
|
+
active: Schema.optional(Schema.Boolean),
|
|
42
|
+
currency: Schema.String,
|
|
43
|
+
lookup_key: Schema.optional(Schema.String),
|
|
44
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
45
|
+
nickname: Schema.optional(Schema.String),
|
|
46
|
+
product: ResourceOutputRefSchema,
|
|
47
|
+
recurring: Schema.Struct({
|
|
48
|
+
interval: Schema.Literals(["month", "year"]),
|
|
49
|
+
interval_count: Schema.optional(Schema.Finite),
|
|
50
|
+
}),
|
|
51
|
+
tax_behavior: Schema.optional(
|
|
52
|
+
Schema.Literals(["exclusive", "inclusive", "unspecified"]),
|
|
53
|
+
),
|
|
54
|
+
unit_amount: Schema.Finite,
|
|
55
|
+
}),
|
|
56
|
+
{
|
|
57
|
+
provider: "stripe",
|
|
58
|
+
service: "billing",
|
|
59
|
+
resource: "price",
|
|
60
|
+
operation: "create",
|
|
61
|
+
stateSecretOutputKeys: [],
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
export type StripePriceDeclarationProps = Schema.Schema.Type<
|
|
66
|
+
typeof StripePriceDeclarationPropsSchema
|
|
67
|
+
>;
|
|
68
|
+
|
|
69
|
+
export const StripeRecurringPriceDeclarationPropsSchema = Schema.Struct({
|
|
70
|
+
active: Schema.optional(Schema.Boolean),
|
|
71
|
+
currency: Schema.String,
|
|
72
|
+
lookup_key: Schema.optional(Schema.String),
|
|
73
|
+
metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)),
|
|
74
|
+
nickname: Schema.optional(Schema.String),
|
|
75
|
+
recurring: Schema.Struct({
|
|
76
|
+
interval: Schema.Literals(["month", "year"]),
|
|
77
|
+
interval_count: Schema.optional(Schema.Finite),
|
|
78
|
+
}),
|
|
79
|
+
tax_behavior: Schema.optional(
|
|
80
|
+
Schema.Literals(["exclusive", "inclusive", "unspecified"]),
|
|
81
|
+
),
|
|
82
|
+
unit_amount: Schema.Finite,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
export type StripeRecurringPriceDeclarationProps = Schema.Schema.Type<
|
|
86
|
+
typeof StripeRecurringPriceDeclarationPropsSchema
|
|
87
|
+
>;
|
|
88
|
+
|
|
89
|
+
export const StripePriceOutputsSchema = Schema.Struct({
|
|
90
|
+
PriceId: Schema.String,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
export type StripePriceOutputs = Schema.Schema.Type<
|
|
94
|
+
typeof StripePriceOutputsSchema
|
|
95
|
+
>;
|
|
96
|
+
|
|
97
|
+
export const StripePriceObservedStateSchema = PostPricesOutput;
|
|
98
|
+
|
|
99
|
+
export type StripePriceObservedState = Schema.Schema.Type<
|
|
100
|
+
typeof StripePriceObservedStateSchema
|
|
101
|
+
>;
|
|
102
|
+
|
|
103
|
+
export const stripePriceOutputsFromState = (
|
|
104
|
+
state: StripePriceObservedState,
|
|
105
|
+
): StripePriceOutputs => {
|
|
106
|
+
const outputs: StripePriceOutputs = {
|
|
107
|
+
PriceId: state.id,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return outputs;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export const pendingStripePriceOutputs = (
|
|
114
|
+
logicalId: string,
|
|
115
|
+
): StripePriceOutputs => {
|
|
116
|
+
const outputs: StripePriceOutputs = {
|
|
117
|
+
PriceId: `nomoss:pending:stripe-price-id:${logicalId}`,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return outputs;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const stripePricePropsFromDeclaration = (
|
|
124
|
+
product: string | ResourceOutputRef,
|
|
125
|
+
props: StripeRecurringPriceDeclarationProps,
|
|
126
|
+
): StripePriceProps | StripePriceDeclarationProps => {
|
|
127
|
+
const priceProps = Match.type<string | ResourceOutputRef>().pipe(
|
|
128
|
+
Match.when(Match.string, (stripeProductId) =>
|
|
129
|
+
StripePricePropsSchema.make({
|
|
130
|
+
active: props.active,
|
|
131
|
+
currency: props.currency,
|
|
132
|
+
lookup_key: props.lookup_key,
|
|
133
|
+
metadata: props.metadata,
|
|
134
|
+
nickname: props.nickname,
|
|
135
|
+
product: stripeProductId,
|
|
136
|
+
recurring: props.recurring,
|
|
137
|
+
tax_behavior: props.tax_behavior,
|
|
138
|
+
unit_amount: props.unit_amount,
|
|
139
|
+
}),
|
|
140
|
+
),
|
|
141
|
+
Match.orElse((productRef) =>
|
|
142
|
+
StripePriceDeclarationPropsSchema.make({
|
|
143
|
+
active: props.active,
|
|
144
|
+
currency: props.currency,
|
|
145
|
+
lookup_key: props.lookup_key,
|
|
146
|
+
metadata: props.metadata,
|
|
147
|
+
nickname: props.nickname,
|
|
148
|
+
product: productRef,
|
|
149
|
+
recurring: props.recurring,
|
|
150
|
+
tax_behavior: props.tax_behavior,
|
|
151
|
+
unit_amount: props.unit_amount,
|
|
152
|
+
}),
|
|
153
|
+
),
|
|
154
|
+
)(product);
|
|
155
|
+
|
|
156
|
+
return priceProps;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export class StripePriceLifecycle extends Context.Service<StripePriceLifecycle>()(
|
|
160
|
+
"nomoss/providers/stripe/stripePrice/StripePriceLifecycle",
|
|
161
|
+
{
|
|
162
|
+
make: Effect.gen(function* () {
|
|
163
|
+
const stripeCredentials = yield* Credentials;
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
createPrice: Effect.fn("StripePriceLifecycle.createPrice")(function* (
|
|
167
|
+
props: StripePriceProps,
|
|
168
|
+
) {
|
|
169
|
+
const price = yield* PostPrices(props, {
|
|
170
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
171
|
+
}).pipe(Effect.provideService(Credentials, stripeCredentials));
|
|
172
|
+
|
|
173
|
+
return price;
|
|
174
|
+
}),
|
|
175
|
+
|
|
176
|
+
readPrice: Effect.fn("StripePriceLifecycle.readPrice")(function* (
|
|
177
|
+
priceId: string,
|
|
178
|
+
) {
|
|
179
|
+
const input: GetPricesPriceInput = {
|
|
180
|
+
price: priceId,
|
|
181
|
+
};
|
|
182
|
+
const price = yield* GetPricesPrice(input, {
|
|
183
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
184
|
+
}).pipe(
|
|
185
|
+
Effect.provideService(Credentials, stripeCredentials),
|
|
186
|
+
Effect.flatMap((output) =>
|
|
187
|
+
Schema.decodeUnknownEffect(StripePriceObservedStateSchema)(
|
|
188
|
+
output,
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
Effect.map((output) => Option.some(output)),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
return price;
|
|
195
|
+
}),
|
|
196
|
+
|
|
197
|
+
deactivatePrice: Effect.fn("StripePriceLifecycle.deactivatePrice")(
|
|
198
|
+
function* (priceId: string) {
|
|
199
|
+
/**
|
|
200
|
+
* Stripe keeps Price terms immutable for active subscriptions.
|
|
201
|
+
* Replacement clears the lookup key on the old Price so future
|
|
202
|
+
* Checkout sessions resolve only the newly declared terms.
|
|
203
|
+
*/
|
|
204
|
+
const input: PostPricesPriceInput = {
|
|
205
|
+
price: priceId,
|
|
206
|
+
active: false,
|
|
207
|
+
lookup_key: "",
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
yield* PostPricesPrice(input, {
|
|
211
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
212
|
+
}).pipe(Effect.provideService(Credentials, stripeCredentials));
|
|
213
|
+
},
|
|
214
|
+
),
|
|
215
|
+
};
|
|
216
|
+
}),
|
|
217
|
+
},
|
|
218
|
+
) {}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { Context, Effect, Match } from "effect";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type PlanDecision,
|
|
5
|
+
ResourceCommand,
|
|
6
|
+
ResourceCommandResult,
|
|
7
|
+
ResourceCommandUnsupported,
|
|
8
|
+
} from "../../core/lifecycle.js";
|
|
9
|
+
import {
|
|
10
|
+
ResourceModel,
|
|
11
|
+
type ResourceNode,
|
|
12
|
+
readResourceSchemaAnnotation,
|
|
13
|
+
} from "../../core/model.js";
|
|
14
|
+
import {
|
|
15
|
+
StripePriceLifecycle,
|
|
16
|
+
type StripePriceOutputs,
|
|
17
|
+
StripePriceOutputsSchema,
|
|
18
|
+
StripePricePropsSchema,
|
|
19
|
+
stripePriceOutputsFromState,
|
|
20
|
+
} from "./stripePrice.js";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Stripe Price commands create a new Price when desired pricing inputs change,
|
|
24
|
+
* matching Stripe's immutable amount and recurring terms model.
|
|
25
|
+
*/
|
|
26
|
+
export class StripePriceResourcePolicy extends Context.Service<StripePriceResourcePolicy>()(
|
|
27
|
+
"nomoss/providers/stripe/stripePriceResourcePolicy",
|
|
28
|
+
{
|
|
29
|
+
make: Effect.gen(function* () {
|
|
30
|
+
const lifecycle = yield* StripePriceLifecycle;
|
|
31
|
+
const model = yield* ResourceModel;
|
|
32
|
+
const schema = yield* readResourceSchemaAnnotation(
|
|
33
|
+
StripePricePropsSchema,
|
|
34
|
+
).pipe(Effect.fromOption);
|
|
35
|
+
|
|
36
|
+
const resourceNode = Effect.fn("StripePriceResourcePolicy.resourceNode")(
|
|
37
|
+
function* (node: ResourceNode, outputs: StripePriceOutputs) {
|
|
38
|
+
const props = yield* model.decodeProps(node, StripePricePropsSchema);
|
|
39
|
+
const appliedNode = yield* model.nodeFromResource({
|
|
40
|
+
key: node.key,
|
|
41
|
+
propsSchema: StripePricePropsSchema,
|
|
42
|
+
outputsSchema: StripePriceOutputsSchema,
|
|
43
|
+
props,
|
|
44
|
+
outputs,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
return appliedNode;
|
|
48
|
+
},
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const create = Effect.fn("StripePriceResourcePolicy.create")(function* (
|
|
52
|
+
node: ResourceNode,
|
|
53
|
+
) {
|
|
54
|
+
const props = yield* model.decodeProps(node, StripePricePropsSchema);
|
|
55
|
+
const price = yield* lifecycle.createPrice(props);
|
|
56
|
+
const outputs = stripePriceOutputsFromState(price);
|
|
57
|
+
const appliedNode = yield* resourceNode(node, outputs);
|
|
58
|
+
|
|
59
|
+
return ResourceCommandResult.Created({ node: appliedNode });
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const replace = Effect.fn("StripePriceResourcePolicy.replace")(function* (
|
|
63
|
+
node: ResourceNode,
|
|
64
|
+
current: ResourceNode,
|
|
65
|
+
) {
|
|
66
|
+
const outputs = yield* model.decodeOutputs(
|
|
67
|
+
current,
|
|
68
|
+
StripePriceOutputsSchema,
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
yield* lifecycle.deactivatePrice(outputs.PriceId);
|
|
72
|
+
|
|
73
|
+
return yield* create(node).pipe(
|
|
74
|
+
Effect.map(({ node: appliedNode }) =>
|
|
75
|
+
ResourceCommandResult.Updated({ node: appliedNode }),
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
const destroy = Effect.fn("StripePriceResourcePolicy.destroy")(function* (
|
|
81
|
+
node: ResourceNode,
|
|
82
|
+
) {
|
|
83
|
+
const outputs = yield* model.decodeOutputs(
|
|
84
|
+
node,
|
|
85
|
+
StripePriceOutputsSchema,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
yield* lifecycle.deactivatePrice(outputs.PriceId);
|
|
89
|
+
|
|
90
|
+
return ResourceCommandResult.Destroyed({ node });
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const apply = Effect.fn("StripePriceResourcePolicy.apply")(function* (
|
|
94
|
+
decision: PlanDecision,
|
|
95
|
+
) {
|
|
96
|
+
return yield* Match.value(decision).pipe(
|
|
97
|
+
Match.tagsExhaustive({
|
|
98
|
+
Create: ({ node }) => create(node),
|
|
99
|
+
Update: ({ node, current }) => replace(node, current),
|
|
100
|
+
Destroy: ({ node }) => destroy(node),
|
|
101
|
+
NoOp: () =>
|
|
102
|
+
Effect.fail(
|
|
103
|
+
new ResourceCommandUnsupported({
|
|
104
|
+
command: ResourceCommand.Apply({ decision }),
|
|
105
|
+
}),
|
|
106
|
+
),
|
|
107
|
+
Repair: () =>
|
|
108
|
+
Effect.fail(
|
|
109
|
+
new ResourceCommandUnsupported({
|
|
110
|
+
command: ResourceCommand.Apply({ decision }),
|
|
111
|
+
}),
|
|
112
|
+
),
|
|
113
|
+
Delete: () =>
|
|
114
|
+
Effect.fail(
|
|
115
|
+
new ResourceCommandUnsupported({
|
|
116
|
+
command: ResourceCommand.Apply({ decision }),
|
|
117
|
+
}),
|
|
118
|
+
),
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
schema,
|
|
125
|
+
create,
|
|
126
|
+
replace,
|
|
127
|
+
destroy,
|
|
128
|
+
apply,
|
|
129
|
+
execute: Effect.fn("StripePriceResourcePolicy.execute")(function* (
|
|
130
|
+
command: ResourceCommand,
|
|
131
|
+
) {
|
|
132
|
+
return yield* Match.value(command).pipe(
|
|
133
|
+
Match.tagsExhaustive({
|
|
134
|
+
Read: () =>
|
|
135
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
136
|
+
Diff: () =>
|
|
137
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
138
|
+
Apply: ({ decision }) => apply(decision),
|
|
139
|
+
Create: ({ node }) => create(node),
|
|
140
|
+
Update: () =>
|
|
141
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
142
|
+
Delete: () =>
|
|
143
|
+
Effect.fail(new ResourceCommandUnsupported({ command })),
|
|
144
|
+
Destroy: ({ node }) => destroy(node),
|
|
145
|
+
}),
|
|
146
|
+
);
|
|
147
|
+
}),
|
|
148
|
+
};
|
|
149
|
+
}),
|
|
150
|
+
},
|
|
151
|
+
) {}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Credentials } from "@distilled.cloud/stripe";
|
|
2
|
+
import type {
|
|
3
|
+
GetProductsIdInput,
|
|
4
|
+
PostProductsIdInput,
|
|
5
|
+
} from "@distilled.cloud/stripe/Operations";
|
|
6
|
+
import {
|
|
7
|
+
GetProductsId,
|
|
8
|
+
PostProducts,
|
|
9
|
+
PostProductsId,
|
|
10
|
+
PostProductsIdInput as PostProductsIdInputSchema,
|
|
11
|
+
PostProductsInput,
|
|
12
|
+
PostProductsOutput,
|
|
13
|
+
} from "@distilled.cloud/stripe/Operations";
|
|
14
|
+
import { type Cause, Context, Data, Effect, Option, Schema } from "effect";
|
|
15
|
+
|
|
16
|
+
import { annotateResourceSchema } from "../../core/model.js";
|
|
17
|
+
import { StripeApiVersion } from "./stripeAccount.js";
|
|
18
|
+
|
|
19
|
+
export const StripeProductPropsSchema = annotateResourceSchema(
|
|
20
|
+
PostProductsInput,
|
|
21
|
+
{
|
|
22
|
+
provider: "stripe",
|
|
23
|
+
service: "billing",
|
|
24
|
+
resource: "product",
|
|
25
|
+
operation: "create",
|
|
26
|
+
stateSecretOutputKeys: [],
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
export type StripeProductProps = Schema.Schema.Type<
|
|
31
|
+
typeof StripeProductPropsSchema
|
|
32
|
+
>;
|
|
33
|
+
|
|
34
|
+
export const StripeProductUpdatePropsSchema = PostProductsIdInputSchema;
|
|
35
|
+
|
|
36
|
+
export type StripeProductUpdateProps = Schema.Schema.Type<
|
|
37
|
+
typeof StripeProductUpdatePropsSchema
|
|
38
|
+
>;
|
|
39
|
+
|
|
40
|
+
export const StripeProductOutputsSchema = Schema.Struct({
|
|
41
|
+
ProductId: Schema.String,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export type StripeProductOutputs = Schema.Schema.Type<
|
|
45
|
+
typeof StripeProductOutputsSchema
|
|
46
|
+
>;
|
|
47
|
+
|
|
48
|
+
export const StripeProductObservedStateSchema = PostProductsOutput;
|
|
49
|
+
|
|
50
|
+
export type StripeProductObservedState = Schema.Schema.Type<
|
|
51
|
+
typeof StripeProductObservedStateSchema
|
|
52
|
+
>;
|
|
53
|
+
|
|
54
|
+
export class StripeProductUpdateFailed extends Data.TaggedError(
|
|
55
|
+
"StripeProductUpdateFailed",
|
|
56
|
+
)<{
|
|
57
|
+
readonly cause: Cause.Cause<unknown>;
|
|
58
|
+
}> {}
|
|
59
|
+
|
|
60
|
+
export class StripeProductDestroyFailed extends Data.TaggedError(
|
|
61
|
+
"StripeProductDestroyFailed",
|
|
62
|
+
)<{
|
|
63
|
+
readonly cause: Cause.Cause<unknown>;
|
|
64
|
+
}> {}
|
|
65
|
+
|
|
66
|
+
export const stripeProductOutputsFromState = (
|
|
67
|
+
state: StripeProductObservedState,
|
|
68
|
+
): StripeProductOutputs => {
|
|
69
|
+
const outputs: StripeProductOutputs = {
|
|
70
|
+
ProductId: state.id,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return outputs;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const pendingStripeProductOutputs = (
|
|
77
|
+
logicalId: string,
|
|
78
|
+
): StripeProductOutputs => {
|
|
79
|
+
const outputs: StripeProductOutputs = {
|
|
80
|
+
ProductId: `nomoss:pending:stripe-product-id:${logicalId}`,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return outputs;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const stripeProductOutputsFromProps = (
|
|
87
|
+
logicalId: string,
|
|
88
|
+
props: StripeProductProps,
|
|
89
|
+
): StripeProductOutputs => {
|
|
90
|
+
const outputs = Option.match(Option.fromUndefinedOr(props.id), {
|
|
91
|
+
onNone: () => pendingStripeProductOutputs(logicalId),
|
|
92
|
+
onSome: (productId): StripeProductOutputs => ({ ProductId: productId }),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return outputs;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
export class StripeProductLifecycle extends Context.Service<StripeProductLifecycle>()(
|
|
99
|
+
"nomoss/providers/stripe/stripeProduct/StripeProductLifecycle",
|
|
100
|
+
{
|
|
101
|
+
make: Effect.gen(function* () {
|
|
102
|
+
const stripeCredentials = yield* Credentials;
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
createProduct: Effect.fn("StripeProductLifecycle.createProduct")(
|
|
106
|
+
function* (props: StripeProductProps) {
|
|
107
|
+
const product = yield* PostProducts(props, {
|
|
108
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
109
|
+
}).pipe(Effect.provideService(Credentials, stripeCredentials));
|
|
110
|
+
|
|
111
|
+
return product;
|
|
112
|
+
},
|
|
113
|
+
),
|
|
114
|
+
|
|
115
|
+
readProduct: Effect.fn("StripeProductLifecycle.readProduct")(function* (
|
|
116
|
+
productId: string,
|
|
117
|
+
) {
|
|
118
|
+
const input: GetProductsIdInput = {
|
|
119
|
+
id: productId,
|
|
120
|
+
};
|
|
121
|
+
const product = yield* GetProductsId(input, {
|
|
122
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
123
|
+
}).pipe(
|
|
124
|
+
Effect.provideService(Credentials, stripeCredentials),
|
|
125
|
+
Effect.flatMap((output) =>
|
|
126
|
+
Schema.decodeUnknownEffect(StripeProductObservedStateSchema)(
|
|
127
|
+
output,
|
|
128
|
+
),
|
|
129
|
+
),
|
|
130
|
+
Effect.map((output) => Option.some(output)),
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
return product;
|
|
134
|
+
}),
|
|
135
|
+
|
|
136
|
+
updateProduct: Effect.fn("StripeProductLifecycle.updateProduct")(
|
|
137
|
+
function* (input: StripeProductUpdateProps) {
|
|
138
|
+
const product = yield* PostProductsId(input, {
|
|
139
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
140
|
+
}).pipe(
|
|
141
|
+
Effect.provideService(Credentials, stripeCredentials),
|
|
142
|
+
Effect.catchCause((cause) =>
|
|
143
|
+
Effect.fail(new StripeProductUpdateFailed({ cause })),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
return product;
|
|
148
|
+
},
|
|
149
|
+
),
|
|
150
|
+
|
|
151
|
+
deactivateProduct: Effect.fn(
|
|
152
|
+
"StripeProductLifecycle.deactivateProduct",
|
|
153
|
+
)(function* (productId: string) {
|
|
154
|
+
const input: PostProductsIdInput = {
|
|
155
|
+
id: productId,
|
|
156
|
+
active: false,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
yield* PostProductsId(input, {
|
|
160
|
+
apiVersion: StripeApiVersion.Dahlia,
|
|
161
|
+
}).pipe(
|
|
162
|
+
Effect.provideService(Credentials, stripeCredentials),
|
|
163
|
+
Effect.catchCause((cause) =>
|
|
164
|
+
Effect.fail(new StripeProductDestroyFailed({ cause })),
|
|
165
|
+
),
|
|
166
|
+
);
|
|
167
|
+
}),
|
|
168
|
+
};
|
|
169
|
+
}),
|
|
170
|
+
},
|
|
171
|
+
) {}
|