import {z} from 'zod' export type Duration = {count?: number | undefined, unit?: ('UNIT_UNSPECIFIED' | 'MONTH' | 'DAY' | 'HOUR') | undefined}; export type Extension = {duration?: Duration | undefined, partnerUserToken?: string | undefined}; export type ExtendSubscriptionRequest = {extension?: Extension | undefined, requestId?: string | undefined}; export type CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdExtendArgs = {partnersId: string, subscriptionsId: string, body: ExtendSubscriptionRequest}; export type CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdExtendOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const extendSubscriptionResponse = z.object({cycleEndTime: z.string().optional(), freeTrialEndTime: z.string().optional(), renewalTime: z.string().optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * [Opt-in only] Most partners should be on auto-extend by default. Extends a subscription service for their customers on an ongoing basis for the subscription to remain active and renewable. It should be called directly by the partner using service accounts. * * POST /v1/partners/{partnersId}/subscriptions/{subscriptionsId}:extend */ export const createApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdExtend = async (args: CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdExtendArgs, options?: CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdExtendOptions) => { const {baseUrl = "https://paymentsresellersubscription.googleapis.com", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/partners/${encodeURIComponent(String(args.partnersId))}/subscriptions/${encodeURIComponent(String(args.subscriptionsId))}:extend${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(extendSubscriptionResponse, await res.json()) };