@contentstack/types-generator
Version:
Contentstack type definition generation library
192 lines (188 loc) • 4.22 kB
JavaScript
import {
generateTS,
generateTSFromContentTypes
} from "./chunk-UV7YT23N.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",
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"
};
async function graphqlTS({
apiKey,
token,
region,
environment,
branch,
namespace,
host
}) {
var _a, _b, _c, _d, _e, _f;
try {
if (!token || !apiKey || !environment || !region) {
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) {
throw {
type: "validation",
error_message: `GraphQL content delivery api unavailable for '${region}' region and no custom host provided`
};
}
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 (error.response.status === 412) {
throw {
error_message: "Unauthorized: The apiKey, token or environment is not valid."
};
} else {
let details = "";
if (((_d = (_c = (_b = (_a = error.response.data.errors[0]) == null ? void 0 : _a.extensions) == null ? void 0 : _b.errors) == null ? void 0 : _c[0]) == null ? void 0 : _d.code) === "SCHEMA_BUILD_ERROR") {
details = error.response.data.errors[0].extensions.errors[0].details.map((element) => element.error).join("\n");
}
throw {
error_message: details ? details : (_f = (_e = error.response.data.errors[0]) == null ? void 0 : _e.extensions) == null ? void 0 : _f.errors[0].message
};
}
}
}
export {
generateTS,
generateTSFromContentTypes,
graphqlTS
};