Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement apis #15

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vercel/
node_modules/
13 changes: 13 additions & 0 deletions api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { VercelRequest, VercelResponse } from "@vercel/node";

import { data, prepareLevel1 } from './utils/data';

export default function (req: VercelRequest, res: VercelResponse) {
const level1s = [];

for (const level1 of data) {
level1s.push(prepareLevel1(level1, { req }))
}

return res.send({ level1s });
}
24 changes: 24 additions & 0 deletions api/level1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { VercelRequest, VercelResponse } from "@vercel/node";

import { data, prepareLevel1, prepareLevel2 } from './utils/data';
import { getGis } from "./utils/vercel";

export default function (req: VercelRequest, res: VercelResponse) {
const { level1Id } = req.query;
const gis = getGis(req);
const level2s = [];

for (const level1 of data) {
if (level1.level1_id !== level1Id) continue;
for (const level2 of level1.level2s) {
level2s.push(prepareLevel2(level2, { level1, req }))
}

return res.send({
level1: prepareLevel1(level1, { gis }),
level2s
});
}

return res.status(404).send({ level2s });
}
28 changes: 28 additions & 0 deletions api/level2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { VercelRequest, VercelResponse } from "@vercel/node";

import { data, prepareLevel1, prepareLevel2, prepareLevel3 } from './utils/data';
import { getGis } from "./utils/vercel";

export default function (req: VercelRequest, res: VercelResponse) {
const { level1Id, level2Id } = req.query;
const gis = getGis(req);
const level3s = [];

for (const level1 of data) {
if (level1.level1_id !== level1Id) continue;
for (const level2 of level1.level2s) {
if (level2.level2_id !== level2Id) continue;
for (const level3 of level2.level3s) {
level3s.push(prepareLevel3(level3, { level1, level2, req }))
}

return res.send({
level1: prepareLevel1(level1),
level2: prepareLevel2(level2, { gis, level1 }),
level3s
});
}
}

return res.status(404).send({ level3s });
}
127 changes: 127 additions & 0 deletions api/utils/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { VercelRequest } from "@vercel/node";
import { getSiteUrl } from "./vercel";

interface Level1 {
level1_id: string
name: string
type: string
level2s: Level2[]
}

interface Level2 {
level2_id: string
name: string
type: string
level3s: Level3[]
}

interface Level3 {
level3_id: string
name: string
type: string
}

type Bbox = number[]
type Coordinates = number[][][]
interface PreparedData {
id: string
name: string
type: string

detail: string

bbox?: Bbox
geometry?: {
type: string
coordinates: Coordinates
}
}

interface GisLevel1 {
level1_id: string
name: string

bbox: Bbox
type: string
coordinates: Coordinates

level2s: GisLevel2[]
}

interface GisLevel2 {
level2_id: string
name: string

bbox: Bbox
type: string
coordinates: Coordinates
}

const dvhcvn = require("../../data/dvhcvn") as { data: Level1[] };
export const data = dvhcvn.data;

const gis: Record<string, GisLevel1> = {};
for (const level1 of data) {
gis[level1.level1_id] = require(`../../data/gis/${level1.level1_id}.json`);
}

interface PrepareLevel1Options {
gis?: boolean
req?: VercelRequest
}
export function prepareLevel1(level1: Level1, options: PrepareLevel1Options = {}): PreparedData {
const { level1_id: id, name, type } = level1
const { req } = options
const detail = req ? `${getSiteUrl(req)}/api/${id}` : undefined

let prepared: PreparedData = { id, name, type, detail }

if (options.gis === true) {
const thisGis = gis[id];
const { bbox, coordinates, type } = thisGis || {};
prepared = { ...prepared, bbox, geometry: { coordinates, type } }
}

return prepared;
}

interface PrepareLevel2Options {
gis?: boolean
level1?: Level1
req?: VercelRequest
}
export function prepareLevel2(level2: Level2, options: PrepareLevel2Options = {}): PreparedData {
const { level2_id: id, name, type } = level2

const { level1, req } = options
const detail = (level1 && req) ? `${getSiteUrl(req)}/api/${level1.level1_id}/${id}` : undefined

let prepared: PreparedData = { id, name, type, detail }

if (level1 && options.gis === true) {
const { level2s: gisLevel2s } = gis[level1.level1_id];
for (const gisLevel2 of gisLevel2s) {
if (gisLevel2.level2_id !== id) continue;
const { bbox, coordinates, type } = gisLevel2
prepared = { ...prepared, bbox, geometry: { coordinates, type } }
}
}

return prepared;
}

interface PrepareLevel3Options {
level1?: Level1
level2?: Level2
req?: VercelRequest
}
export function prepareLevel3(level3: Level3, options: PrepareLevel3Options = {}): PreparedData {
const { level3_id: id, name, type } = level3

const { level1, level2, req } = options
const detail = (level1 && level2 && req) ?
`${getSiteUrl(req)}/api/${level1.level1_id}/${level2.level2_id}/${id}` :
undefined

return { id, name, type, detail }
}
16 changes: 16 additions & 0 deletions api/utils/vercel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { VercelRequest } from "@vercel/node";

export function getGis(req: VercelRequest): boolean {
const { gis } = req.query;
switch (gis) {
case '1':
case 'yes':
return true;
}

return false;
}

export function getSiteUrl(req: VercelRequest) {
return `${req.headers['x-forwarded-proto']}://${req.headers['x-forwarded-host']}`
}
10 changes: 5 additions & 5 deletions now.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"builds": [
{ "src": "data/gis/*.json", "use": "@now/static" },
{ "src": "data/sorted.json", "use": "@now/static" },
{ "src": "demo/parser/api/index.ts", "use": "@now/node" },
{ "src": "demo/*.html", "use": "@now/static" },
{ "src": "demo/*.js", "use": "@now/static" }
{ "src": "api/*.ts", "use": "@now/node" }
],
"rewrites": [
{ "source": "/api/:level1Id", "destination": "/api/level1.ts" },
{ "source": "/api/:level1Id/:level2Id", "destination": "/api/level2.ts" }
],
"version": 2
}
Loading