{"version":3,"file":"get.mjs","sources":["../../../../../src/runtime/internals/operations/get.ts"],"sourcesContent":["import { authModeParams, buildGraphQLVariables, flattenItems, generateGraphQLDocument, getCustomHeaders, initializeModel, } from '../APIClient';\nimport { handleSingularGraphQlError } from './utils';\nimport { selfAwareAsync } from '../../utils';\nimport { extendCancellability } from '../cancellation';\nimport { createUserAgentOverride } from '../ai/getCustomUserAgentDetails';\nexport function getFactory(client, modelIntrospection, model, operation, getInternals, useContext = false, customUserAgentDetails) {\n    const getWithContext = (contextSpec, arg, options) => {\n        return _get(client, modelIntrospection, model, arg, options, operation, getInternals, contextSpec, customUserAgentDetails);\n    };\n    const get = (arg, options) => {\n        return _get(client, modelIntrospection, model, arg, options, operation, getInternals, undefined, customUserAgentDetails);\n    };\n    return useContext ? getWithContext : get;\n}\nfunction _get(client, modelIntrospection, model, arg, options, operation, getInternals, context, customUserAgentDetails) {\n    return selfAwareAsync(async (resultPromise) => {\n        const { name } = model;\n        const query = generateGraphQLDocument(modelIntrospection, model, operation, options);\n        const variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);\n        const auth = authModeParams(client, getInternals, options);\n        const headers = getCustomHeaders(client, getInternals, options?.headers);\n        const userAgentOverride = createUserAgentOverride(customUserAgentDetails);\n        try {\n            const basePromise = context\n                ? client.graphql(context, {\n                    ...auth,\n                    query,\n                    variables,\n                }, headers)\n                : client.graphql({\n                    ...auth,\n                    query,\n                    variables,\n                    ...userAgentOverride,\n                }, headers);\n            const extendedPromise = extendCancellability(basePromise, resultPromise);\n            const { data, extensions } = await extendedPromise;\n            // flatten response\n            if (data) {\n                const [key] = Object.keys(data);\n                const flattenedResult = flattenItems(modelIntrospection, name, data[key]);\n                if (flattenedResult === null) {\n                    return { data: null, extensions };\n                }\n                else if (options?.selectionSet) {\n                    return { data: flattenedResult, extensions };\n                }\n                else {\n                    // TODO: refactor to avoid destructuring here\n                    const [initialized] = initializeModel(client, name, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken, !!context);\n                    return { data: initialized, extensions };\n                }\n            }\n            else {\n                return { data: null, extensions };\n            }\n        }\n        catch (error) {\n            /**\n             * The `data` type returned by `error` here could be:\n             * 1) `null`\n             * 2) an empty object\n             * 3) \"populated\" but with a `null` value `{ getPost: null }`\n             * 4) an actual record `{ getPost: { id: '1', title: 'Hello, World!' } }`\n             */\n            const { data, errors } = error;\n            /**\n             * `data` is not `null`, and is not an empty object:\n             */\n            if (data && Object.keys(data).length !== 0 && errors) {\n                const [key] = Object.keys(data);\n                const flattenedResult = flattenItems(modelIntrospection, name, data[key]);\n                /**\n                 * `flattenedResult` could be `null` here (e.g. `data: { getPost: null }`)\n                 * if `flattenedResult`, result is an actual record:\n                 */\n                if (flattenedResult) {\n                    if (options?.selectionSet) {\n                        return { data: flattenedResult, errors };\n                    }\n                    else {\n                        // TODO: refactor to avoid destructuring here\n                        const [initialized] = initializeModel(client, name, [flattenedResult], modelIntrospection, auth.authMode, auth.authToken, !!context);\n                        return { data: initialized, errors };\n                    }\n                }\n                else {\n                    // was `data: { getPost: null }`)\n                    return handleSingularGraphQlError(error);\n                }\n            }\n            else {\n                // `data` is `null`:\n                return handleSingularGraphQlError(error);\n            }\n        }\n    });\n}\n"],"names":[],"mappings":";;;;;;AAKO,SAAS,UAAU,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,GAAG,KAAK,EAAE,sBAAsB,EAAE;AACnI,IAAI,MAAM,cAAc,GAAG,CAAC,WAAW,EAAE,GAAG,EAAE,OAAO,KAAK;AAC1D,QAAQ,OAAO,IAAI,CAAC,MAAM,EAAE,kBAAkB,EAAE,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,WAAW,EAAE,sBAAsB,CAAC;AAClI,IAAI,CAAC;AACL,IAAI,MAAM,GAAG,GAAG,CAAC,GAAG,EAAE,OAAO,KAAK;AAClC,QAAQ,OAAO,IAAI,CAAC,MAAM,EAAE,k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