UNPKG

@contentstack/types-generator

Version:

Contentstack type definition generation library

204 lines (200 loc) 5.3 kB
import { ERROR_MESSAGES, createLogger, generateTS, generateTSFromContentTypes } from "./chunk-HX73TDLN.mjs"; // src/graphqlTS/index.ts import { schemaToInterfaces, generateNamespace } from "@gql2ts/from-schema"; // src/graphqlTS/queries.ts var introspectionQuery = `query IntrospectionQuery{ __schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name description locations args { ...InputValue } } } } fragment FullType on __Type { kind name description fields(includeDeprecated: true) { name description args { ...InputValue } type { ...TypeRef } isDeprecated deprecationReason } inputFields { ...InputValue } interfaces { ...TypeRef } enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason } possibleTypes { ...TypeRef } } fragment InputValue on __InputValue { name description type { ...TypeRef } defaultValue } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name ofType { kind name } } } } } } } }`; // src/graphqlTS/index.ts import axios from "axios"; var GRAPHQL_REGION_URL_MAPPING = { US: "https://graphql.contentstack.com/stacks", EU: "https://eu-graphql.contentstack.com/stacks", AU: "https://au-graphql.contentstack.com/stacks", AZURE_NA: "https://azure-na-graphql.contentstack.com/stacks", AZURE_EU: "https://azure-eu-graphql.contentstack.com/stacks", GCP_NA: "https://gcp-na-graphql.contentstack.com/stacks", GCP_EU: "https://gcp-eu-graphql.contentstack.com/stacks" }; async function graphqlTS({ apiKey, token, region, environment, branch, namespace, host, logger: loggerInstance }) { var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n; const logger = createLogger(loggerInstance); try { if (!token || !apiKey || !environment || !region) { logger.error(ERROR_MESSAGES.MISSING_REQUIRED_PARAMS); logger.warn(ERROR_MESSAGES.REQUIRED_PARAMS_LIST); throw { type: "validation", error_message: "Please provide all the required params (token, apiKey, environment, region)" }; } let config = { method: "post", maxBodyLength: Infinity, url: host ? `https://${host}/stacks/${apiKey}` : `${GRAPHQL_REGION_URL_MAPPING[region]}/${apiKey}`, headers: { access_token: token, branch: "" }, data: { query: introspectionQuery }, params: { environment } }; if (branch) { config.headers.branch = branch; } if (!GRAPHQL_REGION_URL_MAPPING[region] && !host) { logger.error(ERROR_MESSAGES.UNSUPPORTED_REGION(region)); logger.warn(ERROR_MESSAGES.SUPPORTED_REGIONS); logger.warn(ERROR_MESSAGES.CUSTOM_HOST_OPTION); throw { type: "validation", error_message: ERROR_MESSAGES.GRAPHQL_API_UNAVAILABLE(region) }; } const result = await axios.request(config); let schema; if (namespace) { schema = generateNamespace(namespace, result == null ? void 0 : result.data); } else { schema = schemaToInterfaces(result == null ? void 0 : result.data); } return schema; } catch (error) { if (error.type === "validation") { throw { error_message: error.error_message }; } if (error.message && !error.response) { throw { error_message: error.message }; } if (((_a = error.response) == null ? void 0 : _a.status) === 412) { throw { error_message: ERROR_MESSAGES.INVALID_CREDENTIALS_GRAPHQL }; } else { let details = ""; if (((_c = (_b = error.response) == null ? void 0 : _b.data) == null ? void 0 : _c.errors) && Array.isArray(error.response.data.errors) && error.response.data.errors.length > 0 && ((_e = (_d = error.response.data.errors[0]) == null ? void 0 : _d.extensions) == null ? void 0 : _e.errors) && Array.isArray(error.response.data.errors[0].extensions.errors) && error.response.data.errors[0].extensions.errors.length > 0 && ((_f = error.response.data.errors[0].extensions.errors[0]) == null ? void 0 : _f.code) === "SCHEMA_BUILD_ERROR") { details = ((_g = error.response.data.errors[0].extensions.errors[0].details) == null ? void 0 : _g.map((element) => element.error).join("\n")) || ""; } const errorMessage = (_n = (_m = (_l = (_k = (_j = (_i = (_h = error.response) == null ? void 0 : _h.data) == null ? void 0 : _i.errors) == null ? void 0 : _j[0]) == null ? void 0 : _k.extensions) == null ? void 0 : _l.errors) == null ? void 0 : _m[0]) == null ? void 0 : _n.message; throw { error_message: details || errorMessage || ERROR_MESSAGES.GRAPHQL_SCHEMA_ERROR }; } } } export { generateTS, generateTSFromContentTypes, graphqlTS };