Skip to content

Commit 4a759fc

Browse files
authored
refactor(entitlement): update V2 API to better conform to new standards (#3401)
1 parent 7cf71f2 commit 4a759fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6006
-2832
lines changed

api/api.gen.go

Lines changed: 1500 additions & 1191 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/client/go/client.gen.go

Lines changed: 1470 additions & 1160 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/client/javascript/src/client/schemas.ts

Lines changed: 300 additions & 13 deletions
Large diffs are not rendered by default.

api/client/javascript/src/zod/index.ts

Lines changed: 267 additions & 22 deletions
Large diffs are not rendered by default.

api/openapi.cloud.yaml

Lines changed: 341 additions & 14 deletions
Large diffs are not rendered by default.

api/openapi.yaml

Lines changed: 341 additions & 14 deletions
Large diffs are not rendered by default.

api/spec/src/entitlements/v2/customer.tsp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface CustomerEntitlements {
2929
@operationId("createCustomerEntitlementV2")
3030
post(
3131
@path customerIdOrKey: ULIDOrExternalKey,
32-
@body entitlement: OpenMeter.Entitlements.EntitlementCreateInputs,
32+
@body entitlement: EntitlementV2CreateInputs,
3333
): {
3434
@statusCode _: 201;
3535
@body body: EntitlementV2;
@@ -88,7 +88,7 @@ interface CustomerEntitlements {
8888
override(
8989
@path customerIdOrKey: ULIDOrExternalKey,
9090
@path entitlementIdOrFeatureKey: ULIDOrExternalKey,
91-
@body entitlement: EntitlementCreateInputs,
91+
@body entitlement: EntitlementV2CreateInputs,
9292
):
9393
| {
9494
@statusCode _: 201;
@@ -117,7 +117,7 @@ interface CustomerEntitlement {
117117
...OpenMeter.QueryPagination,
118118
...OpenMeter.QueryLimitOffset,
119119
...OpenMeter.QueryOrdering<GrantOrderBy>,
120-
): OpenMeter.PaginatedResponse<Grant> | OpenMeter.CommonErrors;
120+
): OpenMeter.PaginatedResponse<GrantV2> | OpenMeter.CommonErrors;
121121

122122
/**
123123
* Grants define a behavior of granting usage for a metered entitlement. They can have complicated recurrence and rollover rules, thanks to which you can define a wide range of access patterns with a single grant, in most cases you don't have to periodically create new grants. You can only issue grants for active metered entitlements.
@@ -139,10 +139,10 @@ interface CustomerEntitlement {
139139
createCustomerEntitlementGrant(
140140
@path customerIdOrKey: ULIDOrExternalKey,
141141
@path entitlementIdOrFeatureKey: ULIDOrKey,
142-
@body grant: GrantCreateInput,
142+
@body grant: GrantCreateInputV2,
143143
): {
144144
@statusCode _: 201;
145-
@body body: Grant;
145+
@body body: GrantV2;
146146
} | OpenMeter.CommonErrors | OpenMeter.ConflictError;
147147

148148
/**

api/spec/src/entitlements/v2/entitlements.tsp

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ union EntitlementV2 {
8787
boolean: EntitlementBooleanV2,
8888
}
8989

90+
/**
91+
* Create inputs for entitlement
92+
*/
93+
@discriminated(#{ envelope: "none", discriminatorPropertyName: "type" })
94+
@friendlyName("EntitlementV2CreateInputs")
95+
union EntitlementV2CreateInputs {
96+
metered: EntitlementMeteredV2CreateInputs,
97+
static: EntitlementStaticCreateInputs,
98+
boolean: EntitlementBooleanCreateInputs,
99+
}
100+
90101
/**
91102
* Customer fields for entitlements
92103
*/
@@ -105,6 +116,70 @@ model EntitlementCustomerFields {
105116
customerId: ULID;
106117
}
107118

119+
/**
120+
* Issue after reset
121+
*/
122+
@friendlyName("IssueAfterReset")
123+
model IssueAfterReset {
124+
/**
125+
* The initial grant amount
126+
*/
127+
@summary("Initial grant amount")
128+
@minValue(0)
129+
amount: float64;
130+
131+
/**
132+
* The priority of the issue after reset
133+
*/
134+
@summary("Issue grant after reset priority")
135+
@minValue(1)
136+
@maxValue(255)
137+
priority?: uint8 = 1;
138+
}
139+
140+
/**
141+
* Create inputs for metered entitlement
142+
*/
143+
@friendlyName("EntitlementMeteredV2CreateInputs")
144+
model EntitlementMeteredV2CreateInputs {
145+
...OmitProperties<
146+
OpenMeter.Entitlements.EntitlementMeteredCreateInputs,
147+
"isUnlimited" | "issueAfterReset" | "issueAfterResetPriority"
148+
>;
149+
150+
/**
151+
* You can grant usage automatically alongside the entitlement, the example scenario would be creating a starting balance.
152+
* If an amount is specified here, a grant will be created alongside the entitlement with the specified amount.
153+
* That grant will have it's rollover settings configured in a way that after each reset operation, the balance will return the original amount specified here.
154+
* Manually creating such a grant would mean having the "amount", "minRolloverAmount", and "maxRolloverAmount" fields all be the same.
155+
*/
156+
#deprecated "Use issue.Amount instead, will be removed in next major version."
157+
@minValue(0)
158+
@summary("Initial grant amount")
159+
issueAfterReset?: float64;
160+
161+
/**
162+
* Defines the grant priority for the default grant.
163+
*/
164+
#deprecated "Use issue.Priority instead, will be removed in next major version."
165+
@minValue(1)
166+
@maxValue(255)
167+
@summary("Issue grant after reset priority")
168+
issueAfterResetPriority?: uint8 = 1;
169+
170+
/**
171+
* Issue after reset
172+
*/
173+
@summary("Issue after reset")
174+
issue?: IssueAfterReset;
175+
176+
/**
177+
* Grants
178+
*/
179+
@summary("Grants")
180+
grants?: GrantCreateInputV2[];
181+
}
182+
108183
/**
109184
* Metered entitlements are useful for many different use cases, from setting up usage based access to implementing complex credit systems.
110185
* Access is determined based on feature usage using a balance calculation (the "usage allowance" provided by the issued grants is "burnt down" by the usage).
@@ -113,7 +188,7 @@ model EntitlementCustomerFields {
113188
model EntitlementMeteredV2 {
114189
type: EntitlementType.metered;
115190
...OmitProperties<
116-
EntitlementMeteredCreateInputs,
191+
EntitlementMeteredV2CreateInputs,
117192

118193
| "type"
119194
| "measureUsageFrom"
@@ -122,6 +197,7 @@ model EntitlementMeteredV2 {
122197
| "featureKey"
123198
| "featureId"
124199
| "currentUsagePeriod"
200+
| "grants"
125201
>;
126202
...OmitProperties<
127203
EntitlementSharedFields,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import "@typespec/http";
2+
import "@typespec/rest";
3+
import "@typespec/openapi3";
4+
import "..";
5+
import "../..";
6+
7+
namespace OpenMeter.Entitlements.V2;
8+
9+
/**
10+
* The grant.
11+
*/
12+
@friendlyName("EntitlementGrantV2")
13+
model GrantV2 {
14+
...ResourceTimestamps;
15+
...OmitProperties<GrantCreateInputV2, "recurrence">;
16+
17+
/**
18+
* Readonly unique ULID identifier.
19+
*/
20+
@example("01ARZ3NDEKTSV4RRFFQ69G5FAV")
21+
@visibility(Lifecycle.Read)
22+
id: ULID;
23+
24+
/**
25+
* The unique entitlement ULID that the grant is associated with.
26+
*/
27+
@example("01ARZ3NDEKTSV4RRFFQ69G5FAV")
28+
@visibility(Lifecycle.Read)
29+
entitlementId: string;
30+
31+
/**
32+
* The next time the grant will recurr.
33+
*/
34+
@example(DateTime.fromISO("2023-01-01T01:01:01.001Z"))
35+
nextRecurrence?: DateTime;
36+
37+
/**
38+
* The time the grant expires.
39+
*/
40+
@example(DateTime.fromISO("2023-01-01T01:01:01.001Z"))
41+
@visibility(Lifecycle.Read)
42+
expiresAt?: DateTime;
43+
44+
/**
45+
* The time the grant was voided.
46+
*/
47+
@example(DateTime.fromISO("2023-01-01T01:01:01.001Z"))
48+
voidedAt?: DateTime;
49+
50+
/**
51+
* The recurrence period of the grant.
52+
*/
53+
recurrence?: RecurringPeriod;
54+
}
55+
56+
/**
57+
* The grant creation input.
58+
*/
59+
@friendlyName("EntitlementGrantCreateInputV2")
60+
model GrantCreateInputV2 {
61+
...OmitProperties<
62+
OpenMeter.Entitlements.GrantCreateInput,
63+
"maxRolloverAmount" | "metadata" | "expiration"
64+
>;
65+
66+
/**
67+
* Grants are rolled over at reset, after which they can have a different balance compared to what they had before the reset. The default value equals grant amount.
68+
* Balance after the reset is calculated as: Balance_After_Reset = MIN(MaxRolloverAmount, MAX(Balance_Before_Reset, MinRolloverAmount))
69+
*/
70+
@example(100.0)
71+
maxRolloverAmount?: float64;
72+
73+
/**
74+
* The grant expiration definition. If no expiration is provided, the grant can be active indefinitely.
75+
*/
76+
expiration?: ExpirationPeriod;
77+
78+
/**
79+
* The grant metadata.
80+
*/
81+
#deprecated "Use annotations instead, will be removed in next major version."
82+
@example(#{ stripePaymentId: "pi_4OrAkhLvyihio9p51h9iiFnB" })
83+
metadata?: Metadata;
84+
85+
/**
86+
* The grant metadata.
87+
*/
88+
@example(#{ stripePaymentId: "pi_4OrAkhLvyihio9p51h9iiFnB" })
89+
annotations?: Annotations;
90+
}

api/spec/src/entitlements/v2/grants.tsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ interface Grants {
4545
...OpenMeter.QueryPagination,
4646
...OpenMeter.QueryLimitOffset,
4747
...OpenMeter.QueryOrdering<GrantOrderBy>,
48-
): OpenMeter.PaginatedResponse<Grant> | OpenMeter.CommonErrors;
48+
): OpenMeter.PaginatedResponse<GrantV2> | OpenMeter.CommonErrors;
4949
}

0 commit comments

Comments
 (0)