Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"presets": [
"env",
"stage-0"
["@babel/preset-env", {
"targets": {
"node": "22"
}
}]
]
}
23 changes: 11 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,23 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [12.x, 14.x, 16.x, 18.x, 19,x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [22.x]
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache: 'npm'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
run: npm test
- name: Run coverage
run: npm run coverage
run: npm run coverage
- name: Upload coverage to Codecov
if: ${{ matrix.node-version == '16.x' }}
uses: codecov/codecov-action@v3
if: ${{ matrix.node-version == '22.x' }}
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

Expand All @@ -40,11 +39,11 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- run: sudo apt-get install -y oathtool
- uses: actions/setup-node@v2
- uses: actions/setup-node@v4
with:
node-version: 16
node-version: 22
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm config set //registry.npmjs.org/:_authToken ${{secrets.NPM_TOKEN}}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ jobs:

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
node-version: [22.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
Expand Down
152 changes: 59 additions & 93 deletions dist/api.js
Original file line number Diff line number Diff line change
@@ -1,120 +1,86 @@
'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var request = require('request-promise');
var nodeify = require('./utils/nodeify');

var _require = require('./utils/razorpay-utils'),
isNonNullObject = _require.isNonNullObject;

var allowedHeaders = {
const axios = require('axios').default;
const nodeify = require('./utils/nodeify');
const {
isNonNullObject
} = require('./utils/razorpay-utils');
const allowedHeaders = {
"X-Razorpay-Account": "",
"Content-Type": "application/json"
};

function getValidHeaders(headers) {

var result = {};

const result = {};
if (!isNonNullObject(headers)) {

return result;
}

return Object.keys(headers).reduce(function (result, headerName) {

if (allowedHeaders.hasOwnProperty(headerName)) {

result[headerName] = headers[headerName];
}

return result;
}, result);
}

function normalizeError(err) {
throw {
statusCode: err.statusCode,
error: err.error.error
statusCode: err.response.status,
error: err.response.data.error
};
}

var API = function () {
function API(options) {
_classCallCheck(this, API);

this.rq = request.defaults({
baseUrl: options.hostUrl,
json: true,
auth: {
user: options.key_id,
pass: options.key_secret
},
headers: Object.assign({ 'User-Agent': options.ua }, getValidHeaders(options.headers))
});
class API {
constructor(options) {
this.rq = axios.create(this._createConfig(options));
}

_createClass(API, [{
key: 'get',
value: function get(params, cb) {
return nodeify(this.rq.get({
url: params.url,
qs: params.data
}).catch(normalizeError), cb);
}
}, {
key: 'post',
value: function post(params, cb) {
var isNotForm = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;

var request = {
url: params.url,
form: params.data
_createConfig(options) {
const config = {
baseURL: options.hostUrl,
headers: Object.assign({
'User-Agent': options.ua
}, getValidHeaders(options.headers))
};
if (options.key_id && options.key_secret) {
config.auth = {
username: options.key_id,
password: options.key_secret
};

if (isNotForm) {
delete request['form'];
request.body = params.data;
}

return nodeify(this.rq.post(request).catch(normalizeError), cb);
}
}, {
key: 'put',
value: function put(params, cb) {
return nodeify(this.rq.put({
url: params.url,
form: params.data
}).catch(normalizeError), cb);
}
}, {
key: 'patch',
value: function patch(params, cb) {
var request = {
url: params.url,
form: params.data
if (options.oauthToken) {
config.headers = {
'Authorization': `Bearer ${options.oauthToken}`,
...config.headers
};

if (params.data.hasOwnProperty("isbody")) {
delete request['form'];
delete params.data.isbody;
request.body = params.data;
}
return nodeify(this.rq.patch(request).catch(normalizeError), cb);
}
}, {
key: 'delete',
value: function _delete(params, cb) {
return nodeify(this.rq.delete({
url: params.url
}).catch(normalizeError), cb);
}
}]);

return API;
}();
return config;
}
version = 'v1';
getEntityUrl(params) {
return params.hasOwnProperty('version') ? `/${params.version}${params.url}` : `/${this.version}${params.url}`;
}
get(params, cb) {
return nodeify(this.rq.get(this.getEntityUrl(params), {
params: params.data
}).catch(normalizeError), cb);
}
post(params, cb) {
return nodeify(this.rq.post(this.getEntityUrl(params), params.data).catch(normalizeError), cb);
}

// postFormData method for file uploads.
postFormData(params, cb) {
return nodeify(this.rq.post(this.getEntityUrl(params), params.formData, {
'headers': {
'Content-Type': 'multipart/form-data'
}
}).catch(normalizeError), cb);
}
put(params, cb) {
return nodeify(this.rq.put(this.getEntityUrl(params), params.data).catch(normalizeError), cb);
}
patch(params, cb) {
return nodeify(this.rq.patch(this.getEntityUrl(params), params.data).catch(normalizeError), cb);
}
delete(params, cb) {
return nodeify(this.rq.delete(this.getEntityUrl(params)).catch(normalizeError), cb);
}
}
module.exports = API;
132 changes: 132 additions & 0 deletions dist/oAuthTokenClient.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import API, { INormalizeError } from "./types/api"

declare namespace OAuthTokenClient {

interface OAuthTokenBaseRequestBody {
/**
* Unique client identifier.
*/
client_id: string;
/**
* Client secret string.
*/
client_secret: string;
}

interface InitiateAuthorisationRequest extends Pick<OAuthTokenBaseRequestBody, 'client_id'>{
/**
* Specifies that the application is requesting an
* authorisation code grant. possible value is `code`.
*/
response_type: string;
/**
* Callback URL used by Razorpay to redirect after the user approves or denies the authorisation request.
* The client should whitelist the `redirect_uri`.
*/
redirect_uri: string;
/**
* Defines what access your application is requesting from the user. You can request multiple scopes
* by separating with a space.
* possible values is `read_only` or `read_write`.
*/
scope: string | string[];
/**
* Check [doc](https://razorpay.com/docs/partners/technology-partners/onboard-businesses/integrate-oauth/integration-steps/#query-parameters) for required params
*/
state: string;
onboarding_signature?: string;
}

interface OAuthTokenRequest extends OAuthTokenBaseRequestBody {
/**
* Defines the grant type for the request. possible value is `authorization_code` or `refresh_token`
*/
grant_type?: "authorization_code" | "refresh_token";
/**
* Specifies the same `redirect_uri` used in the authorisation request.
*/
redirect_uri?: string;
/**
* Decoded authorisation code received in the last step.
*/
code?: string;
/**
* The type of mode. possible values is `test` or `live`.
*/
mode?: "test" | "live";
/**
* Used to refresh the access token when it expires.
*/
refresh_token?: string;
/**
* The type of token for the request. possible value is `access_token` or `refresh_token`.
*/
token_type_hint?: "access_token" | "refresh_token";
/**
* The token whose access should be revoked.
*/
token?: string;
}

interface OAuthTokenTokenResponse {
/**
* A public key is used only for public routes such as Checkout or Payments.
*/
public_token: string;
/**
* Defines the type of access token. possible value is `Bearer`
*/
token_type: string;
/**
* Integer representing the TTL of the access token in seconds.
*/
expires_in: number;
/**
* A private key used to access sub-merchant resources on Razorpay.
* used for server-to-server calls only.
*/
access_token: string;
/**
* Used to refresh the access token when it expires.
*/
refresh_token:string;
/**
* Identifies the sub-merchant ID who granted the authorisation.
*/
razorpay_account_id: string;
}
}

declare class OAuthTokenClient extends API{
constructor()

getEntityUrl(): string;
/**
* Initiate Authorisation Using URL
* @param param - Check [doc](https://razorpay.com/docs/partners/technology-partners/onboard-businesses/integrate-oauth/integration-steps/#query-parameters) for required params
*/
generateAuthUrl(param: OAuthTokenClient.InitiateAuthorisationRequest): string;

/**
* Get access token
* @param param - Check [doc](https://razorpay.com/docs/partners/technology-partners/onboard-businesses/integrate-oauth/integration-steps/#request-parameters) for required params
*/
getAccessToken(param: OAuthTokenClient.OAuthTokenRequest): Promise<OAuthTokenClient.OAuthTokenTokenResponse>;
getAccessToken(param: OAuthTokenClient.OAuthTokenRequest, callback: (err: INormalizeError | null, data: OAuthTokenClient.OAuthTokenTokenResponse) => void): void

/**
* Get refresh token
* @param param - Check [doc](https://razorpay.com/docs/partners/technology-partners/onboard-businesses/integrate-oauth/integration-steps/#refresh-token-api) for required params
*/
refreshToken(param: OAuthTokenClient.OAuthTokenRequest): Promise<OAuthTokenClient.OAuthTokenTokenResponse>;
refreshToken(param: OAuthTokenClient.OAuthTokenRequest, callback: (err: INormalizeError | null, data: OAuthTokenClient.OAuthTokenTokenResponse) => void): void

/**
* Revoke token
* @param param - Check [doc](https://razorpay.com/docs/partners/technology-partners/onboard-businesses/integrate-oauth/integration-steps/#revoke-token-api) for required params
*/
revokeToken(param: OAuthTokenClient.OAuthTokenRequest): Promise<{ message: string;}>;
revokeToken(param: OAuthTokenClient.OAuthTokenRequest, callback: (err: INormalizeError | null, data: { message: string;}) => void): void
}

export = OAuthTokenClient
Loading
Loading