Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

teken aja

import axios, { AxiosResponse } from 'axios'

let tekenUrl: string = 'https://apix.sandbox-111094.com/v2/data'
let tekenApiKey: string = process.env.TEKEN_API_KEY

export const tekenProvince = async (): Promise<Record<string, any>> => {
	try {
		const resProvince: AxiosResponse = await axios.get(`${tekenUrl}/province`, {
			headers: { apikey: tekenApiKey }
		})

		return Object.keys(resProvince.data.data).map((val, index) => {
			return { province_id: val, province_name: Object.values(resProvince.data.data)[index] }
		})
	} catch (err: any) {
		return Promise.reject(err)
	}
}

export const tekenDistrict = async (): Promise<Record<string, any>> => {
	try {
		const data: Record<string, any> = await tekenProvince()
		let listData: Record<string, any>[] = []

		const districtData: Record<string, any>[] = await data.map(async ({ province_id }) => {
			const resDistrict: AxiosResponse = await axios.get(`${tekenUrl}/district?province=${province_id}`, {
				headers: { apikey: tekenApiKey }
			})

			const newDistrictData: Record<string, any>[] = []

			Object.keys(resDistrict.data.data).forEach((district, index) => {
				newDistrictData.push({ district_id: district, district_name: Object.values(resDistrict.data.data)[index] })
			})
			return newDistrictData
		})

		for (let i in districtData) {
			listData.push({ province: data[i], district: await districtData.flat(Infinity)[i] })
		}

		return listData
	} catch (err: any) {
		return Promise.reject(err)
	}
}

export const tekenSubDistrict = async (): Promise<Record<string, any>> => {
	try {
		let dukcapil: Record<string, any>[] = []
		const district: Record<string, any> = await tekenDistrict()

		const data: Record<string, any>[] = await district.map(async ({ province, district }) => {
			let listData: Record<string, any>[] = []

			let subdistrictData: Record<string, any>[] = await district.map(async ({ district_id }) => {
				const resSubdistrict: AxiosResponse = await axios.get(
					`${tekenUrl}/subdistrict?province=${province.province_id}&district=${district_id}`,
					{ headers: { apikey: tekenApiKey } }
				)

				const newSubdistrictData: Record<string, any>[] = []
				Object.keys(resSubdistrict.data.data).forEach((subdistrict, index) => {
					newSubdistrictData.push({ subdistrict_id: subdistrict, subdistrict_name: Object.values(resSubdistrict.data.data)[index] })
				})
				return newSubdistrictData
			})

			for (let i in subdistrictData) {
				listData.push({ province: province, subdistrict: await subdistrictData[i] })
			}

			return listData
		})

		for (let i in data) {
			dukcapil.push(await data[i])
		}

		return dukcapil
	} catch (err: any) {
		return Promise.reject(err)
	}
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: a file consists of mcq 
Typescript :: facade design pattern typescript 
Typescript :: nest js get request response by index 
Typescript :: how to ignore a field while desiarilizing in java if its type is not wrong 
Typescript :: ag-grid cell renderer dropdown example 
Typescript :: typescript filter conditionally 
Typescript :: alternative for .include in typescript 
Typescript :: useSortBy 
Typescript :: gravitate a particle to another 
Typescript :: how to check if folder already exists in google drive python 
Typescript :: choose random elements from vector without repetition and adding to another vector c++ 
Typescript :: como acessar um elementRef de um componente 
Typescript :: download objects under a prefix in golang 
Typescript :: vscode pass argument to registerCommand 
Typescript :: lofi hip hop beats cool 
Typescript :: nestjs fail on unknown properties 
Typescript :: R barplots ggplot 
Typescript :: jest aliases typescript storybook 
Typescript :: element of an array is the same as any of the previous elements pandas 
Typescript :: typescript type casting 
Typescript :: which electromagnetic radiation is used for heating and night vision equipment 
Typescript :: re initialize weights keras 
Typescript :: components of .net framework 
Typescript :: Vue In Typescript 
Typescript :: calling from a list elements in steps 
Typescript :: typescript hello world 
Typescript :: what are the three ways for a developer to execute tests in an org 
Typescript :: Coding Exercise: Double Time Modify this recursive program to correctly count down in increments of 2. 
Cpp :: c++ code to print hello world 
Cpp :: How to make two dimensional string in c++ 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =