Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@paxful/sdk-js

Package Overview
Dependencies
Maintainers
6
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@paxful/sdk-js - npm Package Compare versions

Comparing version 0.6.2 to 0.6.3

2

dist/commands/Invoke.d.ts

@@ -40,2 +40,2 @@ import { Request } from "node-fetch";

*/
export default function executeRequestAuthorized(requestBuilder: RequestBuilder, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<any>;
export default function executeRequestAuthorized(requestBuilder: RequestBuilder, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<any>;

@@ -143,30 +143,35 @@ "use strict";

var _this = this;
this.responseParser = function (response) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, response.json()];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); };
this.withHeader("Accept", "application/json");
this.responseParser = function (response) { return __awaiter(_this, void 0, void 0, function () {
var content;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, response.text()];
case 1:
content = _a.sent();
try {
return [2 /*return*/, JSON.parse(content)];
}
catch (e) {
throw Error("Can not parse json response: " + content);
}
return [2 /*return*/];
}
});
}); };
return this;
};
RequestBuilder.prototype.acceptText = function () {
var _this = this;
this.responseParser = function (response) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, response.text()];
case 1: return [2 /*return*/, _a.sent()];
}
}); }); };
this.responseParser = function (response) { return response.text(); };
return this;
};
RequestBuilder.prototype.acceptBinary = function () {
var _this = this;
this.responseParser = function (response) { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, response.blob()
.catch(function () { return (response.arrayBuffer()
.catch(function () { return (response.buffer()); })); })];
});
}); };
this.responseParser = function (response) {
return response.buffer()
.catch(function () { return (response.arrayBuffer()
.catch(function () { return (response.blob()
.catch(function () {
response.text();
})); })); });
};
return this;

@@ -191,18 +196,18 @@ };

return __awaiter(this, void 0, void 0, function () {
var credentials, _a, _b, request, transformResponse;
return __generator(this, function (_c) {
switch (_c.label) {
var credentials, _a, request, transformResponse;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = (credentialStorage === null || credentialStorage === void 0 ? void 0 : credentialStorage.getCredentials());
if (_a) return [3 /*break*/, 2];
credentials = credentialStorage.getCredentials();
if (!!credentials) return [3 /*break*/, 2];
return [4 /*yield*/, ImpersonateCredentials_1.default(config)];
case 1:
_a = (_c.sent());
_c.label = 2;
credentials = _b.sent();
credentialStorage.saveCredentials(credentials);
_b.label = 2;
case 2:
credentials = _a;
_b = requestBuilder
_a = requestBuilder
.withConfig(config)
.withAuthorization(credentials)
.build(), request = _b[0], transformResponse = _b[1];
.build(), request = _a[0], transformResponse = _a[1];
return [2 /*return*/, node_fetch_1.default(request)

@@ -209,0 +214,0 @@ .then(function (response) { return RefreshIfNeeded_1.default(request, response, config, credentialStorage); })

import { Request, Response } from "node-fetch";
import { CredentialStorage } from "../oauth";
import { ApiConfiguration } from "../ApiConfiguration";
export default function validateAndRefresh(request: Request, response: Response, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<Response>;
export default function validateAndRefresh(request: Request, response: Response, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<Response>;

@@ -57,9 +57,5 @@ "use strict";

};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var node_fetch_1 = __importStar(require("node-fetch"));
var url_1 = require("url");
var ImpersonateCredentials_1 = __importDefault(require("./ImpersonateCredentials"));
var refreshAccessToken = function (credentials, config) { return __awaiter(void 0, void 0, void 0, function () {

@@ -100,4 +96,6 @@ var form;

case 0:
if (!credentialStorage) return [3 /*break*/, 2];
credentials = credentialStorage.getCredentials();
if (!credentials) {
throw Error("Misconfiguration: no credentials provided");
}
return [4 /*yield*/, refreshAccessToken(credentials, config)];

@@ -107,8 +105,2 @@ case 1:

credentialStorage.saveCredentials(credentials);
return [3 /*break*/, 4];
case 2: return [4 /*yield*/, ImpersonateCredentials_1.default(config)];
case 3:
credentials = _a.sent();
_a.label = 4;
case 4:
request.headers["Authorization"] = "Bearer " + credentials.accessToken;

@@ -115,0 +107,0 @@ return [2 /*return*/, Promise.resolve(request)];

import { Credentials } from "./Credentials";
export declare interface CredentialStorage {
getCredentials(): Credentials;
getCredentials(): Credentials | undefined;
saveCredentials(credentials: Credentials): Credentials;
}
export declare class InMemoryCredentialStorage implements CredentialStorage {
private credentials?;
constructor();
getCredentials(): Credentials | undefined;
saveCredentials(credentials: Credentials): Credentials;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryCredentialStorage = void 0;
var InMemoryCredentialStorage = /** @class */ (function () {
function InMemoryCredentialStorage() {
// eslint-disable-next-line no-console
console.warn("Warning: at the moment PaxfulApi is using an InMemoryCredentialsStorage, meaning if you restart your application then access token you have received will be lost and SDK will need to fetch a new one. As a result your requests will have additional latency added to them. For production use please create and use a proper implementation that would store the access token in a persistence storage (i.e. database). For more details please see /README.md file.");
}
InMemoryCredentialStorage.prototype.getCredentials = function () {
return this.credentials;
};
InMemoryCredentialStorage.prototype.saveCredentials = function (credentials) {
return this.credentials = credentials;
};
return InMemoryCredentialStorage;
}());
exports.InMemoryCredentialStorage = InMemoryCredentialStorage;
//# sourceMappingURL=CredentialStorage.js.map

@@ -12,3 +12,3 @@ /// <reference types="node" />

private readonly apiConfiguration;
private readonly credentialStorage?;
private readonly credentialStorage;
constructor(configuration: ApiConfiguration, credentialStorage?: CredentialStorage);

@@ -15,0 +15,0 @@ /**

@@ -42,2 +42,3 @@ "use strict";

var Invoke_1 = require("./commands/Invoke");
var CredentialStorage_1 = require("./oauth/CredentialStorage");
/**

@@ -50,3 +51,3 @@ * Interface responsable for exposing Paxful API integration.

this.apiConfiguration = configuration;
this.credentialStorage = credentialStorage;
this.credentialStorage = credentialStorage || new CredentialStorage_1.InMemoryCredentialStorage();
this.validateAndSetDefaultParameters(configuration);

@@ -90,6 +91,3 @@ }

switch (_a.label) {
case 0:
if (!this.credentialStorage)
throw Error("No credentials storage defined.");
return [4 /*yield*/, commands_1.getProfile(this.credentialStorage, this.apiConfiguration)];
case 0: return [4 /*yield*/, commands_1.getProfile(this.credentialStorage, this.apiConfiguration)];
case 1: return [2 /*return*/, _a.sent()];

@@ -253,3 +251,2 @@ }

case 0:
if (!this.credentialStorage) return [3 /*break*/, 2];
_b = (_a = this.credentialStorage).saveCredentials;

@@ -259,4 +256,3 @@ return [4 /*yield*/, credentialsPromise];

_b.apply(_a, [_c.sent()]);
_c.label = 2;
case 2: return [2 /*return*/, credentialsPromise];
return [2 /*return*/, credentialsPromise];
}

@@ -263,0 +259,0 @@ });

{
"name": "@paxful/sdk-js",
"version": "0.6.2",
"version": "0.6.3",
"description": "Paxful javascript SDK",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -15,2 +15,3 @@ import httpMocks from "node-mocks-http";

import FormData from "form-data";
import { InMemoryCredentialStorage } from "../oauth/CredentialStorage";

@@ -291,2 +292,21 @@ const credentials = {

it('I can see an error if response is not a valid json', async function () {
process.env.PAXFUL_DATA_HOST = "";
mockCredentialsStorageReturnValue();
(fetch as unknown as FetchMockSandbox).once({
url: /https:\/\/api\.paxful\.com\/paxful\/v1\/trade\/get/,
method: "POST"
}, {
status: 200,
body: "Some lalala"
}, {
sendAsJson: false
});
const paxfulApi = usePaxful(credentials, credentialStorage);
expect(paxfulApi.invoke(paxfulTradeUrl)).rejects.toThrow(Error);
});
it('I can get my trades', async function () {

@@ -629,2 +649,8 @@ credentialStorage.getCredentials.mockReturnValueOnce({

});
it('Default Credentials Store is InMemoryStore()', async function () {
const paxfulApi = usePaxful({ ...credentials, proxyAgent });
expect(paxfulApi['credentialStorage']).toBeInstanceOf(InMemoryCredentialStorage);
});
});

@@ -102,3 +102,3 @@ import { RequestBuilder } from "../commands/Invoke";

const actual = await transform(response);
expect(actual.constructor.name).toEqual("Blob")
expect(actual.constructor.name).toEqual("Buffer")
});

@@ -105,0 +105,0 @@

@@ -93,4 +93,11 @@ import fetch, { Request, RequestInit, Response } from "node-fetch";

public acceptJson(): RequestBuilder {
this.responseParser = async response => await response.json()
this.withHeader("Accept", `application/json`)
this.withHeader("Accept", "application/json");
this.responseParser = async response => {
const content = await response.text();
try {
return JSON.parse(content);
} catch (e) {
throw Error("Can not parse json response: " + content)
}
}
return this

@@ -100,3 +107,3 @@ }

public acceptText(): RequestBuilder {
this.responseParser = async response => await response.text()
this.responseParser = response => response.text()
return this

@@ -106,8 +113,11 @@ }

public acceptBinary(): RequestBuilder {
this.responseParser = async response => {
return response.blob()
this.responseParser = response => {
return response.buffer()
.catch(() => (
response.arrayBuffer()
.catch(() => (
response.buffer()
response.blob()
.catch(() => {
response.text()
})
))

@@ -133,5 +143,10 @@ ))

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export default async function executeRequestAuthorized(requestBuilder: RequestBuilder, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<any> {
const credentials = credentialStorage?.getCredentials() || await retrieveImpersonatedCredentials(config);
export default async function executeRequestAuthorized(requestBuilder: RequestBuilder, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<any> {
let credentials = credentialStorage.getCredentials();
if (!credentials) {
credentials = await retrieveImpersonatedCredentials(config);
credentialStorage.saveCredentials(credentials);
}
const [request, transformResponse] = requestBuilder

@@ -138,0 +153,0 @@ .withConfig(config)

@@ -36,11 +36,10 @@ import fetch, { Request, Response } from "node-fetch";

const createRequest = async (request: Request, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<Request> => {
let credentials: Credentials;
if(credentialStorage){
credentials = credentialStorage.getCredentials()
credentials = await refreshAccessToken(credentials, config);
credentialStorage.saveCredentials(credentials);
} else {
credentials = await retrieveImpersonatedCredentials(config);
const createRequest = async (request: Request, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<Request> => {
let credentials: Credentials|undefined;
credentials = credentialStorage.getCredentials()
if (!credentials) {
throw Error("Misconfiguration: no credentials provided")
}
credentials = await refreshAccessToken(credentials, config);
credentialStorage.saveCredentials(credentials);
request.headers["Authorization"] = `Bearer ${credentials.accessToken}`;

@@ -51,3 +50,3 @@

const validateIfTokenIsExpired = async (request: Request, response: Response, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<Response> => {
const validateIfTokenIsExpired = async (request: Request, response: Response, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<Response> => {
if (response.status === 401) return await fetch(await createRequest(request, config, credentialStorage));

@@ -57,4 +56,4 @@ return Promise.resolve(response);

export default function validateAndRefresh(request: Request, response: Response, config: ApiConfiguration, credentialStorage?: CredentialStorage): Promise<Response> {
export default function validateAndRefresh(request: Request, response: Response, config: ApiConfiguration, credentialStorage: CredentialStorage): Promise<Response> {
return validateIfTokenIsExpired(request, response, config, credentialStorage);
}
import { Credentials } from "./Credentials";
export declare interface CredentialStorage {
getCredentials(): Credentials;
getCredentials(): Credentials | undefined;
saveCredentials(credentials: Credentials): Credentials;
}
export class InMemoryCredentialStorage implements CredentialStorage {
private credentials?: Credentials;
constructor() {
// eslint-disable-next-line no-console
console.warn("Warning: at the moment PaxfulApi is using an InMemoryCredentialsStorage, meaning if you restart your application then access token you have received will be lost and SDK will need to fetch a new one. As a result your requests will have additional latency added to them. For production use please create and use a proper implementation that would store the access token in a persistence storage (i.e. database). For more details please see /README.md file.")
}
getCredentials(): Credentials | undefined {
return this.credentials;
}
saveCredentials(credentials: Credentials): Credentials {
return this.credentials = credentials;
}
}

@@ -7,2 +7,3 @@ import { Http2ServerResponse } from "http2";

import { AnyJson, containsBinary, InvokeBody, RequestBuilder, AnyPromise } from "./commands/Invoke";
import { InMemoryCredentialStorage } from "./oauth/CredentialStorage";

@@ -16,7 +17,7 @@ /**

private readonly apiConfiguration: ApiConfiguration
private readonly credentialStorage?: CredentialStorage
private readonly credentialStorage: CredentialStorage
constructor(configuration: ApiConfiguration, credentialStorage?: CredentialStorage) {
this.apiConfiguration = configuration;
this.credentialStorage = credentialStorage;
this.credentialStorage = credentialStorage || new InMemoryCredentialStorage();
this.validateAndSetDefaultParameters(configuration);

@@ -57,3 +58,2 @@ }

public async getProfile(): Promise<Profile> {
if(!this.credentialStorage) throw Error("No credentials storage defined.");
return await getProfile(this.credentialStorage, this.apiConfiguration);

@@ -223,7 +223,5 @@ }

private async saveToken(credentialsPromise: Promise<Credentials>): Promise<Credentials> {
if(this.credentialStorage) {
this.credentialStorage.saveCredentials(await credentialsPromise);
}
this.credentialStorage.saveCredentials(await credentialsPromise);
return credentialsPromise;
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc