Skip to content

Commit

Permalink
MIGRATION : JS-SDK to TS-SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuhair2002 committed May 27, 2024
1 parent 38ebd33 commit 395917c
Show file tree
Hide file tree
Showing 5 changed files with 474 additions and 112 deletions.
4 changes: 2 additions & 2 deletions demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { generateTS } from "./src/generateTS/index";
async function fetch() {
try {
const res = await generateTS({
token: "csbb041a11ccf2182c87d74b2d",
apiKey: "bltb07d61d76cca54b3",
token: "csbb041a11ccf2182c87d74b2",
apiKey: "bltb07d61d76cca543",
environment: "development",
region: "US",
tokenType: "delivery",
Expand Down
55 changes: 25 additions & 30 deletions src/generateTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import tsgenFactory from "./factory";
import { defaultInterfaces } from "./stack/builtins";
import { format } from "../format/index";
import { ContentType } from "../types/schema";
import { exit } from "process";

const validRegions = ["US", "EU", "AZURE_NA", "AZURE_EU", "GCP_NA"];

Expand Down Expand Up @@ -52,8 +51,7 @@ export const generateTS = async ({
const globalFieldsQuery = Stack.globalField();
const contentTypes = await contentTypeQuery.find();
const global_fields = await globalFieldsQuery.find();
// console.log(contentTypes);
// exit(0);

const { content_types }: any = contentTypes;

if (!content_types.length) {
Expand Down Expand Up @@ -87,34 +85,31 @@ export const generateTS = async ({
}
}
} catch (error: any) {
throw error;
if (error.type === "validation") {
throw {
error_message: error.error_message,
};
} else {
let errorMessage = "Something went wrong";
if (error.status) {
switch (error.status) {
case 401:
errorMessage =
"Unauthorized: The apiKey, token or region is not valid.";
break;
case 412:
errorMessage =
"Invalid Credentials: Please check the provided apiKey, token and region.";
break;
default:
errorMessage = `${errorMessage}, ${error.error_message}`;
}
}
throw {
error_message: errorMessage,
};
}
}
// } catch (error: any) {
// if (error.type === "validation") {
// throw {
// error_message: error.error_message,
// };
// } else {
// let errorMessage = "Something went wrong";
// if (error.status) {
// switch (error.status) {
// case 401:
// errorMessage =
// "Unauthorized: The apiKey, token or region is not valid.";
// break;
// case 412:
// errorMessage =
// "Invalid Credentials: Please check the provided apiKey, token and region.";
// break;
// default:
// errorMessage = `${errorMessage}, ${error.error_message}`;
// }
// }
// throw {
// error_message: errorMessage,
// };
// }
// }
};

export const generateTSFromContentTypes = async ({
Expand Down
268 changes: 268 additions & 0 deletions tests/integration/generateTS/generateTS.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
import { generateTS } from "../../../src/generateTS/index";
const dotenv = require("dotenv");
dotenv.config({ path: "../../../.env" });

describe("generateTS function", () => {
it("generates type definitions", async () => {
const token = process.env.TOKEN as unknown as any;
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const branch = process.env.BRANCH as unknown as any;

const generatedTS = await generateTS({
token,
apiKey,
environment,
region,
tokenType,
branch,
});

expect(generatedTS).toEqual(expect.stringContaining("interface")); // Check for Output is not undefined
expect(generatedTS).toEqual(expect.stringContaining("Dishes")); // Check for whether typeDef of Content type is included
expect(generatedTS).toEqual(expect.stringContaining("Seo")); // Check for whether typeDef of Global Fields is included
expect(generatedTS).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Check for is Documentation Generated
});

it("generates type definitions without Documentation", async () => {
const token = process.env.TOKEN as unknown as any;
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const includeDocumentation = false;

const generatedTS = await generateTS({
token,
apiKey,
environment,
region,
tokenType,
includeDocumentation,
});

expect(generatedTS).toEqual(expect.stringContaining("interface")); // Check for Output is not undefined
expect(generatedTS).toEqual(expect.stringContaining("Dishes")); // Check for whether typeDef of Content type is included
expect(generatedTS).toEqual(expect.stringContaining("Seo")); // Check for whether typeDef of Global Fields is included
expect(generatedTS).not.toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Check for No Documentation is generated
});

it("generates type definitions with prefix", async () => {
const token = process.env.TOKEN as unknown as any;
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const prefix = "test";

const generatedTS = await generateTS({
token,
apiKey,
environment,
region,
tokenType,
prefix,
});

expect(generatedTS).toEqual(expect.stringContaining("interface")); // Check for Output is not undefined
expect(generatedTS).toMatch(/(?!Dishes)testDishes/); // Check for whether typeDef of Content type is included with test prefix
expect(generatedTS).toMatch(/(?!Seo)testSeo/); // Check for whether typeDef of Global Fields is included with test prefix
expect(generatedTS).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Check for Documentation is generated
});

it("generates type definitions with system fields", async () => {
const token = process.env.TOKEN as unknown as any;
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const systemFields = true;

const generatedTS = await generateTS({
token,
apiKey,
environment,
region,
tokenType,
systemFields,
});

expect(generatedTS).toEqual(expect.stringContaining("interface")); // Check for Output is not undefined
expect(generatedTS).toMatch(/Dishes/); // Check for whether typeDef of Content type is included
expect(generatedTS).toMatch(/Seo/); // Check for whether typeDef of Global Fields is included
expect(generatedTS).toMatch(/export interface SystemFields \{\n/); // Check for whether System Fields are Created
expect(generatedTS).toMatch(/extends SystemFields \{\n/); // Check for whether interfaces have extended system fields interface
expect(generatedTS).toMatch(/\/\*\*.*\*\/\n\s*(export)/); // Check for Documentation is generated
});
});

describe("generateTS function with errors", () => {
it("Check for if all the required fields are provided", async () => {
const token = "";
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const branch = process.env.BRANCH as unknown as any;

try {
await generateTS({
token,
apiKey,
environment,
region,
tokenType,
branch,
});
} catch (err: any) {
expect(err.error_message).toEqual(
"Please provide all the required params (token, tokenType, apiKey, environment, region)"
);
}
});

it("Check for Invalid region", async () => {
const token = process.env.TOKEN as unknown as any;
const apiKey = process.env.APIKEY as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = "wrong" as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const branch = process.env.BRANCH as unknown as any;

try {
await generateTS({
token,
apiKey,
environment,
region,
tokenType,
branch,
});
} catch (err: any) {
expect(err.error_message).toEqual(
"Please provide a valid region, supported regions are : (US, EU, AZURE_NA, AZURE_EU, GCP_NA)"
);
}
});

it("Check for empty content-type response", async () => {
const token = process.env.TOKEN_WITH_NO_CT as unknown as any;
const apiKey = process.env.APIKEY_WITH_NO_CT as unknown as any;
const environment = process.env.ENVIRONMENT as unknown as any;
const region = process.env.REGION as unknown as any;
const tokenType = process.env.TOKENTYPE as unknown as any;
const branch = process.env.BRANCH as unknown as any;

try {
await generateTS({
token,
apiKey,
environment,
region,
tokenType,
branch,
});
} catch (err: any) {
expect(err.error_message).toEqual(
"There are no Content Types in the Stack, please create Content Models to generate type definitions"
);
}
});

// it("Check for invalid api_key", async () => {
// const token = process.env.TOKEN as unknown as any;
// const apiKey = "process.env.APIKEY" as unknown as any;
// const environment = process.env.ENVIRONMENT as unknown as any;
// const region = process.env.REGION as unknown as any;
// const tokenType = process.env.TOKENTYPE as unknown as any;
// const branch = process.env.BRANCH as unknown as any;

// try {
// await generateTS({
// token,
// apiKey,
// environment,
// region,
// tokenType,
// branch,
// });
// } catch (err: any) {
// expect(err.error_message).toEqual(
// "Invalid Credentials: Please check the provided apiKey, token and region."
// );
// }
// });

// it("Check for invalid delivery token", async () => {
// const token = "csqw046a21crf2152b87d64r" as unknown as any;
// const apiKey = process.env.APIKEY as unknown as any;
// const environment = process.env.ENVIRONMENT as unknown as any;
// const region = process.env.REGION as unknown as any;
// const tokenType = process.env.TOKENTYPE as unknown as any;
// const branch = process.env.BRANCH as unknown as any;

// try {
// await generateTS({
// token,
// apiKey,
// environment,
// region,
// tokenType,
// branch,
// });
// } catch (err: any) {
// expect(err.error_message).toEqual(
// "Unauthorized: The apiKey, token or region is not valid."
// );
// }
// });

// // Branch Support is Not there in TS SDK
// it("Check for default error with invalid branch", async () => {
// const token = process.env.TOKEN as unknown as any;
// const apiKey = process.env.APIKEY as unknown as any;
// const environment = process.env.ENVIRONMENT as unknown as any;
// const region = process.env.REGION as unknown as any;
// const tokenType = process.env.TOKENTYPE as unknown as any;
// const branch = "mai" as unknown as any;

// try {
// await generateTS({
// token,
// apiKey,
// environment,
// region,
// tokenType,
// branch,
// });
// } catch (err: any) {
// expect(err.error_message).toEqual(
// "Something went wrong, Access denied. You have insufficient permissions to perform operation on this branch 'mai'."
// );
// }
// });

// it("Check for default error like Bad-Request", async () => {
// const token = "process.env.TOKEN" as unknown as any;
// const apiKey = process.env.APIKEY as unknown as any;
// const environment = process.env.ENVIRONMENT as unknown as any;
// const region = process.env.REGION as unknown as any;
// const tokenType = process.env.TOKENTYPE as unknown as any;
// const branch = process.env.BRANCH as unknown as any;

// try {
// await generateTS({
// token,
// apiKey,
// environment,
// region,
// tokenType,
// branch,
// });
// } catch (err: any) {
// expect(err.error_message).toEqual("Something went wrong, Bad Request");
// }
// });
});
Loading

0 comments on commit 395917c

Please sign in to comment.