Skip to content

Commit

Permalink
feat: goodbye scraping, hello static data
Browse files Browse the repository at this point in the history
  • Loading branch information
sooluh committed Apr 30, 2024
1 parent 6b5af11 commit de11b82
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 693 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
dist
node_modules
.vercel
.yarn
.vscode

*.log
7 changes: 0 additions & 7 deletions .gitpod.yml

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data/
1 change: 0 additions & 1 deletion .yarnrc.yml

This file was deleted.

13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<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>
<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>

![@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)

## Requirements

- Node.js `>=16.x`
- Node.js `>= 20.x`
- npm

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

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

| Endpoint | Description | Parameter | Method |
| ---------------------------------------------- | ------------------------------- | ---------- | ------ |
| [`/search`](https://kodepos.vercel.app/search) | To find postcode using keywords | `q` | `GET` |
| | | `province` | `GET` |
| | | `regency` | `GET` |
| | | `district` | `GET` |
| Endpoint | Description | Parameter | Method |
| ---------------------------------------------- | ------------------------------- | --------- | ------ |
| [`/search`](https://kodepos.vercel.app/search) | To find postcode using keywords | `q` | `GET` |

### Example of Use

Expand Down
6 changes: 4 additions & 2 deletions src/controllers/home.ts → app/controllers/home.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import qs from 'node:querystring'
import { KeywordOptions } from '../../types'
import type { FastifyReply, FastifyRequest } from 'fastify'

export const home = async (
request: FastifyRequest<{ Querystring: { q: string } }>,
request: FastifyRequest<{ Querystring: KeywordOptions }>,
reply: FastifyReply
) => {
const { q } = request.query

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

return reply.redirect(302, 'https://github.com/sooluh/kodepos')
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { KeywordOptions } from '../../types'
import { createSpecResponse } from '../helpers/spec'
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

export const search = (app: FastifyInstance) => {
return async (request: FastifyRequest<{ Querystring: KeywordOptions }>, reply: FastifyReply) => {
const { q } = request.query
// TODO: search by province, regency, or district
const data = app.fuse.search(q).sort((a, b) => (a.score || 0) - (b.score || 0))

reply.header('Cache-Control', 's-maxage=86400, stale-while-revalidate=604800')

const result = data.map(({ item }) => item)
const response = createSpecResponse(result)

return reply.send(response)
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions data/kodepos.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions nodemon.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"watch": ["src"],
"watch": ["app", "start"],
"env": {
"NODE_ENV": "development"
},
"ext": "ts,json",
"exec": "ts-node ./src/app.ts"
"exec": "ts-node ./start/app.ts"
}
Loading

0 comments on commit de11b82

Please sign in to comment.