Skip to content

Commit

Permalink
update: update all helpers to .ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Whyu9-9 committed Jul 5, 2023
1 parent ed83649 commit 343dc84
Show file tree
Hide file tree
Showing 21 changed files with 293 additions and 143 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules/
/playwright-report/
/playwright/.cache/
.env
/dist
9 changes: 9 additions & 0 deletions data/test/test/test_data1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"email": "[email protected]",
"address": "p",
"country_id": "ID",
"phone_number": "08873376261",
"phone_code": "62",
"last_name": "wahyu",
"first_name": "wahyu"
}
17 changes: 0 additions & 17 deletions helpers/file_handler.js

This file was deleted.

32 changes: 32 additions & 0 deletions helpers/file_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from "path";
import fs from "fs/promises";

class FileHandler {
async getFile(
file_type: string,
file_status: any,
file_extension: any,
mime_type: any,
is64: boolean
): Promise<{ name: string; mimeType: string; buffer: Buffer }> {
const filePath = path.resolve(
"data/etc",
`${file_type}_example`,
`${file_status}_${mime_type}_${file_type}.${file_extension}`
);

const file: any = await fs.readFile(filePath);

if (is64) {
return file.toString("base64");
}

return {
name: filePath,
mimeType: file_type === "image" ? `image/${mime_type}` : `application/${mime_type}`,
buffer: file,
};
}
}

export default FileHandler;
40 changes: 0 additions & 40 deletions helpers/generate_bearer.js

This file was deleted.

43 changes: 43 additions & 0 deletions helpers/generate_bearer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fetch from "node-fetch";
import dotenv from "dotenv";

class Authenticate {
async getBearer(type: any): Promise<string> {
dotenv.config();

const beforeAsyncResponse = await fetch(process.env.API_LOGIN_URL!, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "*/*",
},
body: JSON.stringify(this.checkType(type))

});

const beforeAsyncResponseJson: Record<string, any> = (await beforeAsyncResponse.json());
const bearer: string = beforeAsyncResponseJson.data.access_token;

return bearer;
}

checkType(type: string): Record<string, any> {
let body: Record<string, any>;

if (type === "username") {
body = {
username: process.env.USERNAME,
password: process.env.PASSWORD,
};
} else {
body = {
email: process.env.EMAIL,
password: process.env.PASSWORD,
};
}

return body;
}
}

export default Authenticate
22 changes: 0 additions & 22 deletions helpers/header-handler.js

This file was deleted.

27 changes: 27 additions & 0 deletions helpers/header-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

import Authenticate from "./generate_bearer";

class HeaderHandler {
async getHeader(
type: any,
multipart: boolean = false
): Promise<Record<string, any>> {
const auth: Authenticate = new Authenticate()
const bearerToken = await auth.getBearer(type)

let headers: Record<string, any> = {
"accept": "*/*",
"Authorization": "Bearer " + bearerToken
};

if (multipart === false) {
headers["Content-Type"] = "application/json";
} else {
headers["Content-Type"] = "application/x-www-form-urlencoded";
}

return headers;
}
}

export default HeaderHandler
11 changes: 7 additions & 4 deletions helpers/randomizer.js → helpers/randomizer.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import fetch from "node-fetch";

class Randomizer {
private wordApiUrl: string;

constructor() {
this.wordApiUrl = "https://random-word-api.herokuapp.com/word?number=1";
}

async randomWord() {
async randomWord(): Promise<string> {
try {
const response = await fetch(this.wordApiUrl);
const words = await response.json();
const words: string[] = await response.json();
return words[0].toUpperCase();
} catch (error) {
console.error(error);
throw error;
}
}

async randomNumber(min, max) {
async randomNumber(min: number, max: number): Promise<number> {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
}

export default Randomizer
export default Randomizer;
75 changes: 74 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@playwright/test": "^1.34.3"
"@playwright/test": "^1.34.3",
"@types/node-fetch": "^2.6.4"
},
"dependencies": {
"allure-commandline": "^2.22.1",
Expand Down
Loading

0 comments on commit 343dc84

Please sign in to comment.