Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions latihan-typescript-bambang-haryo/article.response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.responseData = void 0;
exports.responseData = {
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "Tugas Belajar TypeScript",
"body": "Latihan dulu biar ngerti",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z",
"published": true
},
"author": {
"id": "42",
"type": "people",
"name": "John Dear",
"age": 80,
"role": "contributor"
},
"comments": [{
"name": "John Dear",
"body": "Tes komen",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z"
}]
},
{
"type": "news",
"id": "2",
"attributes": {
"title": "Tugas Belajar TypeScript",
"body": "Latihan dulu biar ngerti",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z",
"published": false
},
"author": {
"id": "42",
"type": "people",
"name": "John Dear",
"age": 80,
"role": "contributor"
},
"comments": [{
"name": "John Dear",
"body": "Tes komen",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z"
}]
}]
};
52 changes: 52 additions & 0 deletions latihan-typescript-bambang-haryo/article.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { DummyResponse } from "./interfaces"

export const responseData: DummyResponse = {
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "Tugas Belajar TypeScript",
"body": "Latihan dulu biar ngerti",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z",
"published": true
},
"author": {
"id": "42",
"type": "people",
"name": "John Dear",
"age": 80,
"role": "contributor"
},
"comments": [{
"name": "John Dear",
"body": "Tes komen",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z"
}]
},
{
"type": "news",
"id": "2",
"attributes": {
"title": "Tugas Belajar TypeScript",
"body": "Latihan dulu biar ngerti",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z",
"published": false
},
"author": {
"id": "42",
"type": "people",
"name": "John Dear",
"age": 80,
"role": "contributor"
},
"comments": [{
"name": "John Dear",
"body": "Tes komen",
"created": "2021-12-27T14:56:29.000Z",
"updated": "2021-12-27T14:56:28.000Z"
}]
}]
}
41 changes: 41 additions & 0 deletions latihan-typescript-bambang-haryo/functions/getItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getItems = void 0;
const article_response_1 = require("../article.response");
let getItems = (type, callback, timeout) => __awaiter(void 0, void 0, void 0, function* () {
let request = () => {
return new Promise((resolve, reject) => {
console.log(`fetching data... [${type}]`);
let resp = article_response_1.responseData.data.filter(({ type: _type }) => _type === type);
setTimeout(() => {
if (resp.length > 0) {
resolve({ status: 200, data: resp });
}
else {
reject({ status: 204, type: type });
}
}, timeout);
});
};
try {
let resp = yield request();
if (resp.status == 200) {
if (typeof callback !== 'undefined') {
callback(resp.data);
}
}
}
catch (error) {
console.log(`error: ${error.status} [${error.type}]`);
}
});
exports.getItems = getItems;
28 changes: 28 additions & 0 deletions latihan-typescript-bambang-haryo/functions/getItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { responseData } from "../article.response";

let getItems = async (type: string, callback?: Function, timeout?: number): Promise<any> => {
let request = () => { return new Promise((resolve, reject) => {
console.log(`fetching data... [${type}]`);
let resp = responseData.data.filter(({type: _type}) => _type === type)
setTimeout(() => {
if(resp.length > 0) {
resolve({status: 200, data: resp})
} else {
reject({status: 204, type: type})
}
}, timeout)
})
}

try {
let resp: any = await request()
if (resp.status == 200) {
if (typeof callback !== 'undefined'){
callback(resp.data)
}
}
} catch(error: any) {
console.log(`error: ${error.status} [${error.type}]`)
}
}
export { getItems }
13 changes: 13 additions & 0 deletions latihan-typescript-bambang-haryo/functions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./getItems"), exports);
1 change: 1 addition & 0 deletions latihan-typescript-bambang-haryo/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./getItems"
9 changes: 9 additions & 0 deletions latihan-typescript-bambang-haryo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const functions_1 = require("./functions");
const getRandomTimeout = () => {
return Math.random() * 3000 + 1000;
};
(0, functions_1.getItems)("news", (resp) => console.log(resp), getRandomTimeout());
(0, functions_1.getItems)("articles", (resp) => console.log(resp), getRandomTimeout());
(0, functions_1.getItems)("ads", (resp) => console.log(resp), getRandomTimeout());
9 changes: 9 additions & 0 deletions latihan-typescript-bambang-haryo/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getItems } from "./functions";

const getRandomTimeout = () => {
return Math.random() * 3000 + 1000
}

getItems("news", (resp: any) => console.log(resp), getRandomTimeout())
getItems("articles", (resp: any) => console.log(resp), getRandomTimeout())
getItems("ads", (resp: any) => console.log(resp), getRandomTimeout())
2 changes: 2 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
7 changes: 7 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Attributes {
title: string;
body: string;
created: string;
updated: string;
published:boolean;
}
2 changes: 2 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/author.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
7 changes: 7 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Author {
id: string;
type: string;
name: string;
age: number;
role: string;
}
2 changes: 2 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
6 changes: 6 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface Comment {
name: string;
body: string;
created: string;
updated: string;
}
2 changes: 2 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/dummyresponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
5 changes: 5 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/dummyresponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Item } from "./item";

export interface DummyResponse {
data: Item[];
}
17 changes: 17 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
__exportStar(require("./attributes"), exports);
__exportStar(require("./author"), exports);
__exportStar(require("./comment"), exports);
__exportStar(require("./item"), exports);
__exportStar(require("./dummyresponse"), exports);
5 changes: 5 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from "./attributes"
export * from "./author"
export * from "./comment"
export * from "./item"
export * from "./dummyresponse"
2 changes: 2 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
11 changes: 11 additions & 0 deletions latihan-typescript-bambang-haryo/interfaces/item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Attributes } from "./attributes"
import { Author } from "./author"
import { Comment } from "./comment"

export interface Item {
type: string;
id: string;
attributes: Attributes;
author: Author;
comments: Comment[];
}
Loading