{"version":3,"file":"generateClient.mjs","sources":["../../../src/internals/generateClient.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { Hub } from '@aws-amplify/core';\nimport { addSchemaToClient, } from '@aws-amplify/data-schema/runtime';\nimport { __amplify, __apiKey, __authMode, __authToken, __endpoint, __headers, getInternals, } from '../types';\nimport { isApiGraphQLConfig } from './utils/runtimeTypeGuards/isApiGraphQLProviderConfig';\nimport { isConfigureEventWithResourceConfig } from './utils/runtimeTypeGuards/isConfigureEventWithResourceConfig';\nimport { cancel, graphql, isCancelError } from './v6';\n/**\n * @private\n *\n * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`\n * compatible context object for config and auth fetching.\n *\n * @param params\n * @returns\n */\nexport function generateClient(params) {\n    const client = {\n        [__amplify]: params.amplify,\n        [__authMode]: params.authMode,\n        [__authToken]: params.authToken,\n        [__apiKey]: 'apiKey' in params ? params.apiKey : undefined,\n        [__endpoint]: 'endpoint' in params ? params.endpoint : undefined,\n        [__headers]: params.headers,\n        graphql,\n        cancel,\n        isCancelError,\n        models: emptyProperty,\n        enums: emptyProperty,\n        queries: emptyProperty,\n        mutations: emptyProperty,\n        subscriptions: emptyProperty,\n    };\n    const apiGraphqlConfig = params.amplify.getConfig().API?.GraphQL;\n    if (client[__endpoint]) {\n        if (!client[__authMode]) {\n            throw new Error('generateClient() requires an explicit `authMode` when `endpoint` is provided.');\n        }\n        if (client[__authMode] === 'apiKey' && !client[__apiKey]) {\n            throw new Error(\"generateClient() requires an explicit `apiKey` when `endpoint` is provided and `authMode = 'apiKey'`.\");\n        }\n    }\n    if (!client[__endpoint]) {\n        if (isApiGraphQLConfig(apiGraphqlConfig)) {\n            addSchemaToClient(client, apiGraphqlConfig, getInternals);\n        }\n        else {\n            // This happens when the `Amplify.configure()` call gets evaluated after the `generateClient()` call.\n            //\n            // Cause: when the `generateClient()` and the `Amplify.configure()` calls are located in\n            // different source files, script bundlers may randomly arrange their orders in the production\n            // bundle.\n            //\n            // With the current implementation, the `client.models` instance created by `generateClient()`\n            // will be rebuilt on every `Amplify.configure()` call that's provided with a valid GraphQL\n            // provider configuration.\n            //\n            // TODO: revisit, and reverify this approach when enabling multiple clients for multi-endpoints\n            // configuration.\n            generateModelsPropertyOnAmplifyConfigure(client);\n        }\n    }\n    return client;\n}\nconst generateModelsPropertyOnAmplifyConfigure = (clientRef) => {\n    Hub.listen('core', coreEvent => {\n        if (!isConfigureEventWithResourceConfig(coreEvent.payload)) {\n            return;\n        }\n        const apiGraphQLConfig = coreEvent.payload.data.API?.GraphQL;\n        if (isApiGraphQLConfig(apiGraphQLConfig)) {\n            addSchemaToClient(clientRef, apiGraphQLConfig, getInternals);\n        }\n    });\n};\nconst emptyProperty = new Proxy({}, {\n    get() {\n        throw new Error('Client could not be generated. This is likely due to `Amplify.configure()` not being called prior to `generateClient()` or because the configuration passed to `Amplify.configure()` is missing GraphQL provider configuration.');\n    },\n});\n"],"names":[],"mappings":";;;;;;;AAAA;AACA;AAOA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,cAAc,CAAC,MAAM,EAAE;AACvC,IAAI,MAAM,MAAM,GAAG;AACnB,QAAQ,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO;AACnC,QAAQ,CAAC,UAAU,GAAG,MAAM,CAAC,QAAQ;AACrC,QAAQ,CAAC,WAAW,GAAG,MAAM,CAAC,SAAS;AACvC,QAAQ,CAAC,QAAQ,GAAG,QAAQ,IAAI,MAAM,GAAG,MAAM,CAAC,MAAM,GAAG,SAAS;AAClE,QAAQ,CAAC,UAAU,GAAG,UAAU,IAAI,MAAM,GAAG,MAAM,CAAC,QAAQ,GAAG,SAAS;AACxE,QAAQ,CAAC,SAAS,GAAG,MAAM,CAAC,OAAO;AACnC,QAAQ,OAAO;AACf,QAAQ,MAAM;AACd,QAAQ,aAAa;AACrB,QAAQ,MAAM,EAAE,aAAa;AAC7B,Q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