Skip to content

Commit de11b82

Browse files
committed
feat: goodbye scraping, hello static data
1 parent 6b5af11 commit de11b82

22 files changed

+335
-693
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
dist
22
node_modules
33
.vercel
4-
.yarn
54
.vscode
6-
75
*.log

.gitpod.yml

-7
This file was deleted.

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data/

.yarnrc.yml

-1
This file was deleted.

README.md

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<a href="https://s.id/standwithpalestine"><img alt="I stand with Palestine" src="https://github.com/Safouene1/support-palestine-banner/blob/master/banner-project.svg" width="100%" /></a>
1+
<a href="https://s.id/standwithpalestine"><img alt="I stand with Palestine" src="https://cdn.jsdelivr.net/gh/Safouene1/support-palestine-banner@master/banner-project.svg" width="100%" /></a>
22

33
![@sooluh/kodepos](https://socialify.git.ci/sooluh/kodepos/image?description=1&descriptionEditable=Indonesian%20postal%20code%20search%20API%20by%20place%20name%2C%20village%20or%20city.&font=Raleway&forks=1&issues=1&logo=https%3A%2F%2Fraw.githubusercontent.com%2Ftwitter%2Ftwemoji%2Fmaster%2Fassets%2Fsvg%2F1f4ee.svg&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark)
44

55
## Requirements
66

7-
- Node.js `>=16.x`
7+
- Node.js `>= 20.x`
88
- npm
99

1010
## Getting Started
@@ -67,12 +67,9 @@ The fastest way to use it privately on PaaS available
6767

6868
Base URL : [`http://localhost:3000`](https://kodepos.vercel.app)
6969

70-
| Endpoint | Description | Parameter | Method |
71-
| ---------------------------------------------- | ------------------------------- | ---------- | ------ |
72-
| [`/search`](https://kodepos.vercel.app/search) | To find postcode using keywords | `q` | `GET` |
73-
| | | `province` | `GET` |
74-
| | | `regency` | `GET` |
75-
| | | `district` | `GET` |
70+
| Endpoint | Description | Parameter | Method |
71+
| ---------------------------------------------- | ------------------------------- | --------- | ------ |
72+
| [`/search`](https://kodepos.vercel.app/search) | To find postcode using keywords | `q` | `GET` |
7673

7774
### Example of Use
7875

src/controllers/home.ts app/controllers/home.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
import qs from 'node:querystring'
2+
import { KeywordOptions } from '../../types'
13
import type { FastifyReply, FastifyRequest } from 'fastify'
24

35
export const home = async (
4-
request: FastifyRequest<{ Querystring: { q: string } }>,
6+
request: FastifyRequest<{ Querystring: KeywordOptions }>,
57
reply: FastifyReply
68
) => {
79
const { q } = request.query
810

911
if (typeof q !== 'undefined' && q.trim() !== '') {
1012
const baseurl = `${request.protocol}://${request.hostname}`
11-
return reply.redirect(301, `${baseurl}/search/?q=${q}`)
13+
return reply.redirect(301, `${baseurl}/search/?${qs.stringify(request.query)}`)
1214
}
1315

1416
return reply.redirect(302, 'https://github.com/sooluh/kodepos')

app/controllers/search.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { KeywordOptions } from '../../types'
2+
import { createSpecResponse } from '../helpers/spec'
3+
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'
4+
5+
export const search = (app: FastifyInstance) => {
6+
return async (request: FastifyRequest<{ Querystring: KeywordOptions }>, reply: FastifyReply) => {
7+
const { q } = request.query
8+
// TODO: search by province, regency, or district
9+
const data = app.fuse.search(q).sort((a, b) => (a.score || 0) - (b.score || 0))
10+
11+
reply.header('Cache-Control', 's-maxage=86400, stale-while-revalidate=604800')
12+
13+
const result = data.map(({ item }) => item)
14+
const response = createSpecResponse(result)
15+
16+
return reply.send(response)
17+
}
18+
}
File renamed without changes.

data/kodepos.json

+1
Large diffs are not rendered by default.

nodemon.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"restartable": "rs",
33
"ignore": [".git", "node_modules/**/node_modules"],
44
"verbose": true,
5-
"watch": ["src"],
5+
"watch": ["app", "start"],
66
"env": {
77
"NODE_ENV": "development"
88
},
99
"ext": "ts,json",
10-
"exec": "ts-node ./src/app.ts"
10+
"exec": "ts-node ./start/app.ts"
1111
}

0 commit comments

Comments
 (0)