import {z} from 'zod' export type GetApiOcapiV1OrdersCompletedOrderIdSurveyArgs = {orderId: string, 'Connect-Region-Code'?: string | undefined}; export type GetApiOcapiV1OrdersCompletedOrderIdSurveyOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const ocapiV1Translation = z.object({languageTag: z.string().min(1), text: z.string().min(1)}); export const ocapiV1Translatable = z.object({text: z.string().min(1), translations: z.array(ocapiV1Translation)}); export const ocapiV1SurveysQuestionsSurveyQuestionType = z.enum(["RatingScale", "Text", "YesNoUnsure"]); export const ocapiV1SurveysQuestionsRatingScaleSurveyQuestionDisplayType = z.enum(["Stars", "Numbers"]); export const ocapiV1SurveysQuestionsRatingScaleSurveyQuestionRange = z.object({minimum: z.number(), maximum: z.number(), step: z.number(), minimumLabel: ocapiV1Translatable.optional(), maximumLabel: ocapiV1Translatable.optional()}); export const ocapiV1SurveysQuestionsSurveyQuestionNotApplicableOptionField = z.object({label: ocapiV1Translatable}); export const ocapiV1SurveysQuestionsSurveyQuestionComment = z.object({label: ocapiV1Translatable, minimumLength: z.number().int(), maximumLength: z.number().int()}); export const ocapiV1SurveysQuestionsFilmRatingScaleSurveyQuestion = z.object({id: z.string().min(1), number: z.number().int(), title: ocapiV1Translatable, isRequired: z.boolean(), type: ocapiV1SurveysQuestionsSurveyQuestionType, displayType: ocapiV1SurveysQuestionsRatingScaleSurveyQuestionDisplayType, range: ocapiV1SurveysQuestionsRatingScaleSurveyQuestionRange, notApplicableOptionField: ocapiV1SurveysQuestionsSurveyQuestionNotApplicableOptionField.optional(), commentField: ocapiV1SurveysQuestionsSurveyQuestionComment.optional(), filmId: z.string().min(1)}); export const ocapiV1SurveysQuestionsFilmSuggestionYesNoUnsureSurveyQuestion = z.object({id: z.string().min(1), number: z.number().int(), title: ocapiV1Translatable, isRequired: z.boolean(), type: ocapiV1SurveysQuestionsSurveyQuestionType, yesOptionLabel: ocapiV1Translatable, noOptionLabel: ocapiV1Translatable, unsureOptionLabel: ocapiV1Translatable, filmId: z.string().min(1)}); export const ocapiV1SurveysQuestionsRatingScaleSurveyQuestion = z.object({id: z.string().min(1), number: z.number().int(), title: ocapiV1Translatable, isRequired: z.boolean(), type: ocapiV1SurveysQuestionsSurveyQuestionType, displayType: ocapiV1SurveysQuestionsRatingScaleSurveyQuestionDisplayType, range: ocapiV1SurveysQuestionsRatingScaleSurveyQuestionRange, notApplicableOptionField: ocapiV1SurveysQuestionsSurveyQuestionNotApplicableOptionField.optional(), commentField: ocapiV1SurveysQuestionsSurveyQuestionComment.optional()}); export const ocapiV1SurveysQuestionsTextSurveyQuestionFormat = z.object({isMultiline: z.boolean(), minimumLength: z.number().int(), maximumLength: z.number().int()}); export const ocapiV1SurveysQuestionsTextSurveyQuestion = z.object({id: z.string().min(1), number: z.number().int(), title: ocapiV1Translatable, isRequired: z.boolean(), type: ocapiV1SurveysQuestionsSurveyQuestionType, format: ocapiV1SurveysQuestionsTextSurveyQuestionFormat}); export const ocapiV1SurveysQuestionsYesNoUnsureSurveyQuestion = z.object({id: z.string().min(1), number: z.number().int(), title: ocapiV1Translatable, isRequired: z.boolean(), type: ocapiV1SurveysQuestionsSurveyQuestionType, yesOptionLabel: ocapiV1Translatable, noOptionLabel: ocapiV1Translatable, unsureOptionLabel: ocapiV1Translatable}); export const ocapiV1SurveysSurveyPage = z.object({number: z.number().int(), questions: z.array(z.union([ocapiV1SurveysQuestionsFilmRatingScaleSurveyQuestion, ocapiV1SurveysQuestionsFilmSuggestionYesNoUnsureSurveyQuestion, ocapiV1SurveysQuestionsRatingScaleSurveyQuestion, ocapiV1SurveysQuestionsTextSurveyQuestion, ocapiV1SurveysQuestionsYesNoUnsureSurveyQuestion])), title: ocapiV1Translatable.optional()}); export const ocapiV1SurveysResponsesRatingScaleSurveyQuestionResponse = z.object({questionId: z.string().min(1), type: ocapiV1SurveysQuestionsSurveyQuestionType, response: z.number().nullable().optional(), isNotApplicableOptionSelected: z.boolean().nullable().optional(), comment: z.string().nullable().optional()}); export const ocapiV1SurveysResponsesTextSurveyQuestionResponse = z.object({questionId: z.string().min(1), type: ocapiV1SurveysQuestionsSurveyQuestionType, response: z.string().nullable().optional()}); export const ocapiV1SurveysResponsesYesNoUnsureResponse = z.enum(["Yes", "No", "Unsure"]); export const ocapiV1SurveysResponsesYesNoUnsureSurveyQuestionResponse = z.object({questionId: z.string().min(1), type: ocapiV1SurveysQuestionsSurveyQuestionType, response: ocapiV1SurveysResponsesYesNoUnsureResponse.optional()}); export const ocapiV1SurveysSurvey = z.object({expiresAt: z.string(), pages: z.array(ocapiV1SurveysSurveyPage), title: ocapiV1Translatable, responses: z.array(z.union([ocapiV1SurveysResponsesRatingScaleSurveyQuestionResponse, ocapiV1SurveysResponsesTextSurveyQuestionResponse, ocapiV1SurveysResponsesYesNoUnsureSurveyQuestionResponse])), isCompleted: z.boolean()}); export const ocapiV1BrowsingMasterDataFilmsFilmExternalIds = z.object({moviexchangeReleaseId: z.string().nullable().optional(), corporateId: z.string().nullable().optional()}); export const ocapiV1SurveysRelatedDataSurveyRelatedFilm = z.object({id: z.string().min(1), title: ocapiV1Translatable, externalIds: ocapiV1BrowsingMasterDataFilmsFilmExternalIds}); export const ocapiV1SurveysRelatedDataSurveyRelatedData = z.object({films: z.array(ocapiV1SurveysRelatedDataSurveyRelatedFilm)}); export const ocapiV1SurveysSurveyDetails = z.object({survey: ocapiV1SurveysSurvey, relatedData: ocapiV1SurveysRelatedDataSurveyRelatedData}); 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 }; /** * Gets the survey for a completed standard order * * GET /ocapi/v1/orders/completed/{orderId}/survey */ export const getApiOcapiV1OrdersCompletedOrderIdSurvey = async (args: GetApiOcapiV1OrdersCompletedOrderIdSurveyArgs, options: GetApiOcapiV1OrdersCompletedOrderIdSurveyOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Connect-Region-Code'] != null) headers.set("Connect-Region-Code", String(args['Connect-Region-Code'])) const url = `${baseUrl.replace(/\/+$/, '')}/ocapi/v1/orders/completed/${encodeURIComponent(String(args.orderId))}/survey${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(ocapiV1SurveysSurveyDetails, await res.json()) };