Skip to content

Commit

Permalink
feat: closes #28
Browse files Browse the repository at this point in the history
  • Loading branch information
sooluh committed Mar 2, 2024
1 parent 8511548 commit 0f51411
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ 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` |
| Endpoint | Description | Parameter | Method |
| ---------------------------------------------- | ------------------------------- | ---------- | ------ |
| [`/search`](https://kodepos.vercel.app/search) | To find postcode using keywords | `q` | `GET` |
| | | `province` | `GET` |
| | | `regency` | `GET` |
| | | `district` | `GET` |

### Example of Use

Expand Down
16 changes: 8 additions & 8 deletions src/controllers/search.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { KeywordOptions } from '../types'
import { createSpecResponse } from '../utils/spec'
import { search as scrape } from '../utils/kodepos'
import { createSpecResponse, sendNotFound } from '../utils/spec'
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

export const search = (app: FastifyInstance) => {
return async (request: FastifyRequest<{ Querystring: { q: string } }>, reply: FastifyReply) => {
const { q } = request.query

if (typeof q === 'undefined' || q.trim() === '') {
return sendNotFound(reply)
}
return async (
request: FastifyRequest<{ Querystring: KeywordOptions & { q: string } }>,
reply: FastifyReply
) => {
const { q, province, regency, district } = request.query

const provider = app.providers.next()
const result = await scrape({ query: q }, provider.value)
const result = await scrape({ query: q, province, regency, district }, provider.value)
const response = createSpecResponse(result)

return reply.send(response)
Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { FastifyInstance, FastifyPluginOptions } from 'fastify'
const load = async (app: FastifyInstance, _: FastifyPluginOptions) => {
const providers = new SequentialRoundRobin<ProviderList>([
{ hostname: 'direktorikodepos.org', segment: 'wilayah' },
{ hostname: 'nomorkodepos.com', segment: 'di' },
{ hostname: 'carikodepos.com', segment: 'daerah' },
])

app.decorate('providers', providers)
Expand Down

0 comments on commit 0f51411

Please sign in to comment.