idempotent-api-middleware v1.1.1
:About
I am an express js API middleware .I will make your API request Idempotent by restricting duplicate API request and store the requestObject , responseObject and status of the request in database through Hasura Graphql.
:Features
- create an active token for each post request
- validate token on each request
:Configuration
import express from 'express'
import { GqlConfig } from '../validator'
const app = express()
const gqlConfig: GqlConfig = new GqlConfig('xxx')
let headers = { 'xxx': 'xxx' }
gqlConfig.setHeaders(headers)
gqlConfig.getActiveToken(ref_id:number ,process:string)
app.use(gqlConfig.validateToken)
:Technologies
The following tools were used in this project:
:Requirements
API Convention
Just add /secured in your API uri \ eg : https://127.0.0.1/secured/book/now
The API uri that contains "/secured" will be validated for idempotency
Before hitting the secured API call getActiveToken(ref_id:number ,process:string ) and get the unique token for the API.
While hitting the secured API set the header 'idempotent-token' and 'process'
eg:
let process = 'PAY_NOW'
let unique_token:string = getActiveToken(ref_id:number ,process:string )
curl --location --request POST 'http://localhost:/order/now' \ --header 'Content-Type: application/json' \ --header 'idempotent-token: unique_token' \ --header 'process: PAY_NOW' \ --data-raw '{ "member_id": 3018, "amount": 10, "created_by": "abc@123.in" }'
Database Configuration
Make your Database like this
- Schema : transaction
Table : token
- Fields : \ id :In---> auto-generated id\ ref_id :Int ---> process respective to transaction (eg: bank_account_no,employee_id)\ token :String ---> auto-generated UUID -> primary key\ process :String ---> process name (Foreign key to )\ is_active :Boolean ---> token status\ is_request_received:Boolean ---> token request received at\ request:String ---> Request\ request_received_at:timestamp\ initiated_at:timestamp\ response:String ---> Response\ completed_at:timestamp\ status:String ---> transction status\ steps:String ---> Error Occured Stage
Table : process
Fields :\ id :Int ---> auto-generated id\ name:String ---> process name (eg: BOOK_ORDER,CANCEL_ORDER )\ entity:String ---> process respective to (eg: ORDER , PAYMENT )
Graphql :
Either use Hasura Graphql Query or Follow Hasura Graphql Query Standards for Graphql Query and mutation for Database actions \ eg:
query getToken(($token: String!) {
transaction_token_by_pk(token: $token) {
ref_id
is_active
is_request_received
request
response
process
initiated_at
completed_at
}
}
:License
This project is un-licensed.
Developed by Sanjay Kumar
Back to top