The best way to control Claude Code, Codex and more from any computer or phone

main
ts 322 lines 11.1 KB
Raw
1 import * as androidpublisher from "@distilled.cloud/gcp/androidpublisher_v3";
2 import { unownedUnlessPrevious } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import { GcpEnvironment } from "../Environment.ts";
9 import type { Providers } from "../Providers.ts";
10 import {
11 DEFAULT_LANGUAGE,
12 defaultInappListings,
13 defaultInappPrice,
14 getInappproduct,
15 jsonEqual,
16 sameText,
17 toDisplayName,
18 toSku,
19 } from "./internal.ts";
20
21 export type InappproductProps = {
22 /**
23 * Play package name of the parent app (for example `com.example.app`).
24 * Immutable — changing it replaces the in-app product.
25 */
26 packageName: string;
27 /**
28 * SKU unique within the app. If omitted, a unique SKU is generated.
29 * Immutable — changing it replaces the product.
30 */
31 sku?: string;
32 /**
33 * Default listing language (BCP-47, for example `en-US`).
34 * @default "en-US"
35 */
36 defaultLanguage?: string;
37 /**
38 * Localized titles and descriptions, keyed by language. A default-language
39 * listing titled after the SKU is added when missing.
40 */
41 listings?: androidpublisher.InAppProductListingMap;
42 /**
43 * Default price in the developer's Checkout currency. Cannot be zero.
44 * @default { currency: "USD", priceMicros: "990000" }
45 */
46 defaultPrice?: androidpublisher.Price;
47 /**
48 * Prices per buyer region.
49 */
50 prices?: androidpublisher.PriceMap;
51 /**
52 * Product status (`active` or `inactive`).
53 */
54 status?: androidpublisher.InAppProductStatusEnum | (string & {});
55 /**
56 * Purchase type. Use `managedUser` for one-time products. Subscriptions
57 * should use `MonetizationSubscription` instead.
58 * @default "managedUser"
59 */
60 purchaseType?: androidpublisher.InAppProductPurchaseTypeEnum | (string & {});
61 /**
62 * Subscription period (ISO 8601). Only for legacy subscription SKUs.
63 */
64 subscriptionPeriod?: string;
65 /**
66 * Trial period (ISO 8601, `P7D` to `P999D`).
67 */
68 trialPeriod?: string;
69 /**
70 * Grace period (ISO 8601) for declined subscription recurrences.
71 */
72 gracePeriod?: string;
73 /**
74 * Tax and compliance settings for managed products.
75 */
76 managedProductTaxesAndComplianceSettings?: androidpublisher.ManagedProductTaxAndComplianceSettings;
77 /**
78 * Tax and compliance settings for legacy subscription SKUs.
79 */
80 subscriptionTaxesAndComplianceSettings?: androidpublisher.SubscriptionTaxAndComplianceSettings;
81 /**
82 * Auto-convert missing regional prices from the default price.
83 * @default true
84 */
85 autoConvertMissingPrices?: boolean;
86 /**
87 * Propagation latency tolerance for product updates.
88 */
89 latencyTolerance?: androidpublisher.PatchInappproductsLatencyToleranceEnum | (string & {});
90 };
91
92 export type Inappproduct = Resource<
93 "GCP.AndroidPublisher.Inappproduct",
94 InappproductProps,
95 {
96 /** Play package name. */
97 packageName: string;
98 /** SKU. */
99 sku: string;
100 /** Project id used when the product was reconciled. */
101 project: string;
102 /** Default listing language. */
103 defaultLanguage: string | undefined;
104 /** Localized listings. */
105 listings: androidpublisher.InAppProductListingMap | undefined;
106 /** Default price. */
107 defaultPrice: androidpublisher.Price | undefined;
108 /** Regional prices. */
109 prices: androidpublisher.PriceMap | undefined;
110 /** Product status. */
111 status: string | undefined;
112 /** Purchase type. */
113 purchaseType: string | undefined;
114 /** Subscription period, if any. */
115 subscriptionPeriod: string | undefined;
116 /** Trial period, if any. */
117 trialPeriod: string | undefined;
118 /** Grace period, if any. */
119 gracePeriod: string | undefined;
120 },
121 never,
122 Providers
123 >;
124
125 /**
126 * A Google Play in-app product (`inappproducts`).
127 *
128 * In-app products have no labels field, so ownership rests on the
129 * deterministic SKU: `read` reports a product it finds without prior state
130 * as unowned (adopt it with `--adopt`). `packageName` and `sku` are identity — changing either replaces the
131 * product. Listings, prices, and status update in place. Prefer
132 * `MonetizationSubscription` for subscriptions; this resource is for
133 * managed one-time products.
134 *
135 * ### Creating an In-app Product
136 * **Example:** Generated SKU
137 * ```typescript
138 * const gem = yield* GCP.AndroidPublisher.Inappproduct("GemPack", {
139 * packageName: "com.example.app",
140 * listings: { "en-US": { title: "Gem pack" } },
141 * });
142 * ```
143 *
144 * **Example:** Explicit SKU and price
145 * ```typescript
146 * const gem = yield* GCP.AndroidPublisher.Inappproduct("GemPack", {
147 * packageName: "com.example.app",
148 * sku: "gem_pack_small",
149 * defaultPrice: { currency: "USD", priceMicros: "1990000" },
150 * listings: { "en-US": { title: "Small gem pack" } },
151 * });
152 * ```
153 *
154 * ### Updating an In-app Product
155 * **Example:** Change the title
156 * ```typescript
157 * // Same logical id as before; only the listing changes.
158 * const gem = yield* GCP.AndroidPublisher.Inappproduct("GemPack", {
159 * packageName: "com.example.app",
160 * listings: { "en-US": { title: "Starter gem pack" } },
161 * });
162 * ```
163 *
164 * @resource
165 * @category AndroidPublisher
166 */
167 export const Inappproduct = Resource<Inappproduct>("GCP.AndroidPublisher.Inappproduct");
168
169 export class InappproductNotResolved extends Data.TaggedError(
170 "GCP.AndroidPublisher.InappproductNotResolved",
171 )<{
172 packageName: string;
173 sku: string;
174 }> {}
175
176 const toAttrs = (product: androidpublisher.InAppProduct, project: string) => ({
177 packageName: product.packageName ?? "",
178 sku: product.sku ?? "",
179 project,
180 defaultLanguage: product.defaultLanguage,
181 listings: product.listings,
182 defaultPrice: product.defaultPrice,
183 prices: product.prices,
184 status: product.status,
185 purchaseType: product.purchaseType,
186 subscriptionPeriod: product.subscriptionPeriod,
187 trialPeriod: product.trialPeriod,
188 gracePeriod: product.gracePeriod,
189 });
190
191 const desiredBody = (input: {
192 packageName: string;
193 sku: string;
194 defaultLanguage: string;
195 listings: androidpublisher.InAppProductListingMap;
196 news: InappproductProps;
197 }): androidpublisher.InAppProduct => ({
198 packageName: input.packageName,
199 sku: input.sku,
200 defaultLanguage: input.defaultLanguage,
201 listings: input.listings,
202 defaultPrice: input.news.defaultPrice ?? defaultInappPrice(),
203 prices: input.news.prices,
204 status: input.news.status,
205 purchaseType: input.news.purchaseType ?? "managedUser",
206 subscriptionPeriod: input.news.subscriptionPeriod,
207 trialPeriod: input.news.trialPeriod,
208 gracePeriod: input.news.gracePeriod,
209 managedProductTaxesAndComplianceSettings: input.news.managedProductTaxesAndComplianceSettings,
210 subscriptionTaxesAndComplianceSettings: input.news.subscriptionTaxesAndComplianceSettings,
211 });
212
213 const needsSync = (
214 current: androidpublisher.InAppProduct,
215 desired: androidpublisher.InAppProduct,
216 ) =>
217 !sameText(current.defaultLanguage, desired.defaultLanguage) ||
218 !jsonEqual(current.listings, desired.listings) ||
219 !jsonEqual(current.defaultPrice, desired.defaultPrice) ||
220 (desired.prices !== undefined && !jsonEqual(current.prices, desired.prices)) ||
221 (desired.status !== undefined && !sameText(current.status, desired.status)) ||
222 (desired.purchaseType !== undefined && !sameText(current.purchaseType, desired.purchaseType)) ||
223 !sameText(current.subscriptionPeriod, desired.subscriptionPeriod) ||
224 !sameText(current.trialPeriod, desired.trialPeriod) ||
225 !sameText(current.gracePeriod, desired.gracePeriod) ||
226 (desired.managedProductTaxesAndComplianceSettings !== undefined &&
227 !jsonEqual(
228 current.managedProductTaxesAndComplianceSettings,
229 desired.managedProductTaxesAndComplianceSettings,
230 )) ||
231 (desired.subscriptionTaxesAndComplianceSettings !== undefined &&
232 !jsonEqual(
233 current.subscriptionTaxesAndComplianceSettings,
234 desired.subscriptionTaxesAndComplianceSettings,
235 ));
236
237 export const InappproductProvider = () =>
238 Provider.succeed(Inappproduct, {
239 stables: ["packageName", "sku", "project", "purchaseType"],
240
241 diff: Effect.fn(function* ({ news, olds, output }) {
242 if (!isResolved(news)) return undefined;
243 const previousPackage = olds?.packageName ?? output?.packageName;
244 if (previousPackage !== undefined && news.packageName !== previousPackage) {
245 return { action: "replace" as const, deleteFirst: false };
246 }
247 const previousSku = olds?.sku ?? output?.sku;
248 if (previousSku !== undefined && news.sku !== undefined && news.sku !== previousSku) {
249 return { action: "replace" as const, deleteFirst: true };
250 }
251 return undefined;
252 }),
253
254 read: Effect.fn(function* ({ id, olds, output, previous }) {
255 const env = yield* GcpEnvironment.current;
256 const packageName = olds?.packageName ?? output?.packageName ?? "";
257 const sku = yield* toSku(id, olds?.sku, output?.sku);
258 const existing = yield* getInappproduct(packageName, sku);
259 if (existing === undefined) return undefined;
260 const attrs = toAttrs(existing, env.project);
261 // No labels field: without prior state the product may not be ours.
262 return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "sku");
263 }),
264
265 reconcile: Effect.fn(function* ({ id, news, output }) {
266 const env = yield* GcpEnvironment.current;
267 const packageName = news.packageName;
268 const sku = yield* toSku(id, news.sku, output?.sku);
269 const defaultLanguage = news.defaultLanguage ?? output?.defaultLanguage ?? DEFAULT_LANGUAGE;
270 const title = yield* toDisplayName(
271 id,
272 news.listings?.[defaultLanguage]?.title,
273 output?.listings?.[defaultLanguage]?.title,
274 );
275 const listings = defaultInappListings(news.listings, title, defaultLanguage);
276 const autoConvertMissingPrices = news.autoConvertMissingPrices ?? true;
277 const desired = desiredBody({
278 packageName,
279 sku,
280 defaultLanguage,
281 listings,
282 news,
283 });
284
285 let current = yield* getInappproduct(packageName, sku);
286
287 if (current === undefined) {
288 const created = yield* androidpublisher
289 .insertInappproducts({
290 packageName,
291 autoConvertMissingPrices,
292 body: desired,
293 })
294 .pipe(Effect.catchTag("Conflict", () => getInappproduct(packageName, sku)));
295 current = created ?? undefined;
296 } else if (needsSync(current, desired)) {
297 current = yield* androidpublisher.patchInappproducts({
298 packageName,
299 sku: current.sku ?? sku,
300 autoConvertMissingPrices,
301 latencyTolerance: news.latencyTolerance,
302 body: desired,
303 });
304 }
305
306 if (current === undefined) {
307 return yield* new InappproductNotResolved({ packageName, sku });
308 }
309
310 return toAttrs(current, env.project);
311 }),
312
313 delete: Effect.fn(function* ({ output }) {
314 if (!output.packageName || !output.sku) return;
315 yield* androidpublisher
316 .deleteInappproducts({
317 packageName: output.packageName,
318 sku: output.sku,
319 })
320 .pipe(Effect.catchTag("NotFound", () => Effect.void));
321 }),
322 });