Skip to content

Commit

Permalink
feat(jabarprov): add a external from jabarprov go id (#2)
Browse files Browse the repository at this point in the history
* feat(jabarprov): add a external from jabar prov

* remove unused in main

* add params fuzziness is false

* remove schema jwt
  • Loading branch information
ayocodingit authored Mar 9, 2023
1 parent ff55901 commit a1a859c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ REDIS_TTL=0
#PRAYER TIMES
PRAYER_TIMES_URL=
PRAYER_TIMES_LOCATION=

#Jabarprov
JABARPROV_URL=
JABARPROV_KEYWORD=
4 changes: 4 additions & 0 deletions src/config/config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ export interface Config {
url: string
location: string
}
jabarprov: {
url: string
keyword: string
}
}
4 changes: 2 additions & 2 deletions src/config/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export default Joi.object({
DB_PASSWORD: Joi.string().required(),
DB_NAME: Joi.string().required(),
DB_AUTH_SOURCE: Joi.string().optional(),
JWT_ACCESS_SECRET: Joi.string().required(),
JWT_ALGORITHM: Joi.string().default('HS256'),
REDIS_HOST: Joi.string().optional(),
REDIS_PORT: Joi.number().optional(),
REDIS_TTL: Joi.number().optional(),
PRAYER_TIMES_URL: Joi.string().uri().required(),
PRAYER_TIMES_LOCATION: Joi.string().required(),
JABARPROV_URL: Joi.string().uri().required(),
JABARPROV_KEYWORD: Joi.string().required(),
})
4 changes: 4 additions & 0 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const config: Config = {
url: env.PRAYER_TIMES_URL,
location: env.PRAYER_TIMES_LOCATION,
},
jabarprov: {
url: env.JABARPROV_URL,
keyword: env.JABARPROV_KEYWORD,
},
}

export default config
38 changes: 38 additions & 0 deletions src/external/jabarprov.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import axios from 'axios'
import winston from 'winston'
import { Config } from '../config/config.interface'
import { Meta, Paginate } from '../helpers/paginate'

class Jabarprov {
constructor(private config: Config, private logger: winston.Logger) {}

public async Search(page: number, limit: number) {
try {
const response = await axios.get(
`${this.config.jabarprov.url}/v1/search`,
{
params: {
q: this.config.jabarprov.keyword,
page,
per_page: limit,
sort_order: 'DESC',
'domain[]': 'news',
fuzziness: false,
},
}
)

const { data, meta } = response.data
const paginate = Paginate({ page, limit })
return {
data,
meta: Meta(paginate, meta.total_count),
}
} catch (error) {
this.logger.error(error)
throw error
}
}
}

export default Jabarprov
7 changes: 6 additions & 1 deletion src/helpers/paginate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ export interface PropPaginate {
limit: number
}

export const Paginate = (query: Record<string, any>): PropPaginate => {
interface QueryPaginate {
page: number
limit: number
}

export const Paginate = (query: QueryPaginate): PropPaginate => {
const limit = Number(query.limit) || 100
const page = Number(query.page) || 1
const offset = limit * (page - 1)
Expand Down

0 comments on commit a1a859c

Please sign in to comment.