Socket
Socket
Sign inDemoInstall

@spinque/query-api

Package Overview
Dependencies
0
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.15.8 to 0.16.0

dist/authentication/TokenCache.d.ts

1

dist/Api.d.ts
import { ApiAuthenticationConfig, ApiConfig, Query, RequestOptions, RequestType, ResponseType, ResultItemTupleTypes } from './types';
export declare const DEFAULT_BASE_URL = "https://rest.spinque.com/";
/**

@@ -3,0 +4,0 @@ * Send queries to the Spinque Query API using fetch.

10

dist/Api.js

@@ -39,3 +39,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = void 0;
exports.Api = exports.DEFAULT_BASE_URL = void 0;
var authentication_1 = require("./authentication");

@@ -45,3 +45,3 @@ var types_1 = require("./types");

// This is the default base URL to the Spinque Query API.
var DEFAULT_BASE_URL = 'https://rest.spinque.com/';
exports.DEFAULT_BASE_URL = 'https://rest.spinque.com/';
/**

@@ -57,3 +57,3 @@ * Send queries to the Spinque Query API using fetch.

*/
this.baseUrl = DEFAULT_BASE_URL;
this.baseUrl = exports.DEFAULT_BASE_URL;
/**

@@ -91,7 +91,7 @@ * Version of the Spinque Query API deployment.

this._authentication = apiConfig.authentication;
this._authenticator = new authentication_1.ClientCredentials(apiConfig.authentication.clientId, apiConfig.authentication.clientSecret, apiConfig.authentication.authServer, apiConfig.authentication.tokenCachePath, apiConfig.baseUrl || DEFAULT_BASE_URL);
this._authenticator = new authentication_1.ClientCredentials(apiConfig.authentication.clientId, apiConfig.authentication.clientSecret, apiConfig.authentication.tokenCache, apiConfig.authentication.authServer, apiConfig.baseUrl);
}
if (apiConfig.authentication.type === 'pkce') {
this._authentication = apiConfig.authentication;
this._authenticator = new authentication_1.PKCE(apiConfig.authentication.clientId, apiConfig.authentication.callback, apiConfig.authentication.authServer, apiConfig.baseUrl || DEFAULT_BASE_URL);
this._authenticator = new authentication_1.PKCE(apiConfig.authentication.clientId, apiConfig.authentication.callback, apiConfig.authentication.authServer, apiConfig.authentication.tokenCache, apiConfig.baseUrl);
}

@@ -98,0 +98,0 @@ }

@@ -0,1 +1,4 @@

import { TokenCache } from './TokenCache';
export declare const DEFAULT_AUTH_SERVER = "https://login.spinque.com/";
export declare const DEFAULT_AUDIENCE = "https://rest.spinque.com/";
/**

@@ -5,7 +8,7 @@ * Abstract class with utitily functions for working with access tokens such as storage

export declare abstract class Authenticator {
_tokenCachePath?: string | undefined;
private _tokenCache;
_authInProgress: boolean;
_accessToken?: string;
_expires?: number;
constructor(_tokenCachePath?: string | undefined);
constructor(_tokenCache: TokenCache);
/**

@@ -12,0 +15,0 @@ * A Promise that delays any operation until an access token is set (with intervals of 50ms)

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Authenticator = void 0;
var __1 = require("..");
exports.Authenticator = exports.DEFAULT_AUDIENCE = exports.DEFAULT_AUTH_SERVER = void 0;
exports.DEFAULT_AUTH_SERVER = 'https://login.spinque.com/';
exports.DEFAULT_AUDIENCE = 'https://rest.spinque.com/';
/**

@@ -9,5 +10,5 @@ * Abstract class with utitily functions for working with access tokens such as storage

var Authenticator = /** @class */ (function () {
function Authenticator(_tokenCachePath) {
function Authenticator(_tokenCache) {
var _this = this;
this._tokenCachePath = _tokenCachePath;
this._tokenCache = _tokenCache;
this._authInProgress = true;

@@ -26,7 +27,7 @@ /**

// First thing to do: check if there's an access token in localStorage
var res = (0, __1.getFromStorage)(this._tokenCachePath);
if (res) {
var cachedToken = this._tokenCache.get();
if (cachedToken) {
// Set it as class property
this._accessToken = res.accessToken;
this._expires = res.expires;
this._accessToken = cachedToken.accessToken;
this._expires = cachedToken.expires;
this._authInProgress = false;

@@ -76,3 +77,3 @@ }

this._expires = Date.now() + expiresIn * 1000;
(0, __1.putInStorage)(this._tokenCachePath, this._accessToken, this._expires);
this._tokenCache.set(this._accessToken, this._expires);
this._authInProgress = false;

@@ -79,0 +80,0 @@ };

import { Authenticator } from './index';
import { TokenCache } from './TokenCache';
/**

@@ -8,6 +9,5 @@ * An Authenticator class for the OAuth 2.0 Client Credentials grant.

private clientSecret;
private authServer?;
private tokenCachePath?;
private baseUrl?;
constructor(clientId: string, clientSecret: string, authServer?: string | undefined, tokenCachePath?: string | undefined, baseUrl?: string | undefined);
private authServer;
private baseUrl;
constructor(clientId: string, clientSecret: string, tokenCache?: TokenCache, authServer?: string, baseUrl?: string);
/**

@@ -14,0 +14,0 @@ * This method fetches an access token using the OAuth 2.0 Client Credentials grant and returns it

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

var utils_2 = require("../utils");
var __1 = require("..");
/**

@@ -85,17 +86,23 @@ * An Authenticator class for the OAuth 2.0 Client Credentials grant.

clientSecret,
// Optional path to store the authentication token and make it persistent through server restarts
tokenCache,
// URL to the Spinque Authorization server, default is https://login.spinque.com/
authServer,
// Optional path to store the authentication token and make it persistent through server restarts
tokenCachePath,
// URL to the Spinque Query API, used as OAuth 2.0 scope, default is https://rest.spinque.com/
baseUrl) {
var _this = _super.call(this, tokenCachePath) || this;
if (authServer === void 0) { authServer = index_1.DEFAULT_AUTH_SERVER; }
if (baseUrl === void 0) { baseUrl = __1.DEFAULT_BASE_URL; }
var _this = this;
if (utils_2.isBrowser) {
throw new Error('The Client Credentials Flow is only allowed for server applications.');
}
if (!tokenCache) {
console.warn("Please supply a TokenCache instance to cache the access token. See the README of @spinque/query-api for more details.");
tokenCache = { get: function () { return null; }, set: function () { } };
}
_this = _super.call(this, tokenCache) || this;
_this.clientId = clientId;
_this.clientSecret = clientSecret;
_this.authServer = authServer;
_this.tokenCachePath = tokenCachePath;
_this.baseUrl = baseUrl;
if (utils_2.isBrowser) {
throw new Error('The Client Credentials Flow is only allowed for server applications.');
}
return _this;

@@ -102,0 +109,0 @@ }

@@ -1,7 +0,6 @@

import { putInStorage, getFromStorage } from './TokenCacheServer';
import { Authenticator } from './Authenticator';
import { ClientCredentials } from './ClientCredentials';
import { PKCE } from './PKCE';
import { TokenCache, localStorageTokenCache } from './TokenCache';
export declare const DEFAULT_AUTH_SERVER = "https://login.spinque.com/";
export declare const DEFAULT_AUDIENCE = "https://rest.spinque.com/";
export { Authenticator, ClientCredentials, PKCE, putInStorage, getFromStorage };
export { Authenticator, TokenCache, ClientCredentials, PKCE, localStorageTokenCache };
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFromStorage = exports.putInStorage = exports.PKCE = exports.ClientCredentials = exports.Authenticator = exports.DEFAULT_AUDIENCE = exports.DEFAULT_AUTH_SERVER = void 0;
var TokenCacheServer_1 = require("./TokenCacheServer");
Object.defineProperty(exports, "putInStorage", { enumerable: true, get: function () { return TokenCacheServer_1.putInStorage; } });
Object.defineProperty(exports, "getFromStorage", { enumerable: true, get: function () { return TokenCacheServer_1.getFromStorage; } });
exports.localStorageTokenCache = exports.PKCE = exports.ClientCredentials = exports.Authenticator = exports.DEFAULT_AUTH_SERVER = void 0;
var Authenticator_1 = require("./Authenticator");

@@ -13,4 +10,5 @@ Object.defineProperty(exports, "Authenticator", { enumerable: true, get: function () { return Authenticator_1.Authenticator; } });

Object.defineProperty(exports, "PKCE", { enumerable: true, get: function () { return PKCE_1.PKCE; } });
var TokenCache_1 = require("./TokenCache");
Object.defineProperty(exports, "localStorageTokenCache", { enumerable: true, get: function () { return TokenCache_1.localStorageTokenCache; } });
exports.DEFAULT_AUTH_SERVER = 'https://login.spinque.com/';
exports.DEFAULT_AUDIENCE = 'https://rest.spinque.com/';
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

import { Authenticator } from './';
import { Authenticator, TokenCache } from './';
/**

@@ -9,4 +9,4 @@ * An Authenticator class for the OAuth 2.0 Authorization Code with PKCE grant.

private authServer?;
private baseUrl?;
constructor(clientId: string, callback: string, authServer?: string | undefined, baseUrl?: string | undefined);
private baseUrl;
constructor(clientId: string, callback: string, authServer?: string | undefined, tokenCache?: TokenCache, baseUrl?: string);
private checkForCallback;

@@ -13,0 +13,0 @@ fetchAccessToken(): Promise<undefined>;

@@ -83,2 +83,4 @@ "use strict";

var utils_2 = require("../utils");
var TokenCache_1 = require("./TokenCache");
var __1 = require("..");
/**

@@ -96,5 +98,9 @@ * An Authenticator class for the OAuth 2.0 Authorization Code with PKCE grant.

authServer,
// Optional path to store the authentication token and make it persistent through server/page reloads
tokenCache,
// URL to the Spinque Query API, used as OAuth 2.0 scope, default is https://rest.spinque.com/
baseUrl) {
var _this = _super.call(this) || this;
if (tokenCache === void 0) { tokenCache = TokenCache_1.localStorageTokenCache; }
if (baseUrl === void 0) { baseUrl = __1.DEFAULT_BASE_URL; }
var _this = _super.call(this, tokenCache) || this;
_this.clientId = clientId;

@@ -139,3 +145,3 @@ _this.callback = callback;

authServer = this.authServer || _1.DEFAULT_AUTH_SERVER;
audience = this.baseUrl || _1.DEFAULT_AUDIENCE;
audience = this.baseUrl || __1.DEFAULT_BASE_URL;
verifier = (0, exports.createRandomString)();

@@ -142,0 +148,0 @@ _a = exports.bufferToBase64UrlEncoded;

@@ -55,3 +55,3 @@ import { Query } from './types';

*/
getResultsQuery(): Query[];
getResultsQuery(excludeModifier?: boolean): Query[];
/**

@@ -58,0 +58,0 @@ * Get the Query objects to retrieve the facet options. When using multiple facets, the facetEndpoint

@@ -136,3 +136,4 @@ "use strict";

*/
FacetedSearch.prototype.getResultsQuery = function () {
FacetedSearch.prototype.getResultsQuery = function (excludeModifier) {
if (excludeModifier === void 0) { excludeModifier = false; }
var q = __spreadArray([

@@ -149,3 +150,3 @@ this.getBaseQuery()

})), false);
if (this._activeModifier !== undefined && this._activeModifier !== null) {
if (!excludeModifier && this._activeModifier !== undefined && this._activeModifier !== null) {
q.push(this._activeModifier);

@@ -152,0 +153,0 @@ }

@@ -0,1 +1,2 @@

import { TokenCache } from './authentication';
/**

@@ -60,6 +61,6 @@ * Configuration of an API to send queries to. Used to instantiate the Api class.

/**
* Path to a file that will store a cached token.
* This is useful during development, when in-memory caching doesn't work due to frequent server restarts.
* Implementation of a TokenCache. Defines a get and set method that put the token in some sort of storage.
* This is especially useful during development, when in-memory caching doesn't work due to frequent server restarts.
*/
tokenCachePath?: string;
tokenCache?: TokenCache;
} | {

@@ -80,2 +81,7 @@ /**

callback: string;
/**
* Implementation of a TokenCache. Defines a get and set method that put the token in some sort of storage.
* This is especially useful during development, when in-memory caching doesn't work due to frequent server restarts.
*/
tokenCache?: TokenCache;
};

@@ -82,0 +88,0 @@ /**

{
"name": "@spinque/query-api",
"version": "0.15.8",
"version": "0.16.0",
"description": "",
"main": "dist/index.js",
"browser": "dist/browser.js",
"types": "dist/index.d.ts",

@@ -8,0 +7,0 @@ "scripts": {

@@ -125,2 +125,29 @@ # @spinque/query-api

#### PKCE flow (for browser applications)
```typescript
import { Api } from "@spinque/query-api";
const api = new Api({
workspace: "my-workspace",
config: "default",
api: "movies",
authentication: {
type: "pkce",
clientId: "abcdefghijklmnopqrstuvwxyz",
callback: "https://my-domain.com/callback",
},
});
const query = {
endpoint: "movie",
parameters: { id: "https://imdb.com/data/movie/tt0209144" },
};
const response = await api.fetch(queries, { count: 10, offset: 0 });
```
Note: the Client ID and Callback URL cannot yet be configured from Spinque Desk.
Ask your system administrator to help you out.
#### Client Credentials flow (for server applications)

@@ -138,4 +165,3 @@

clientId: "abcdefghijklmnopqrstuvwxyz",
clientSecret: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
tokenCachePath: "/tmp/spinque_token_cache",
clientSecret: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
},

@@ -155,35 +181,37 @@ });

The (optional) `tokenCachePath` saves the access token to a file. When the
application is restarted, the token in the file cache will be read and reused if
still valid. This is useful during development, when your application might
restarted often and in-memory cache does not work. Not only does this speed
things up, it also helps stay under your access token usage limits.
It is strongly recommended you use a token cache. `@spinque/query-api` will log a warning message if you don't use a token cache.
#### PKCE flow (for browser applications)
The implementation of the cache is up to you. An example implementation that uses the filesystem:
```typescript
import { Api } from "@spinque/query-api";
export const fileSystemTokenCache: TokenCache = {
get: () => {
try {
const data = fs.readFileSync(TOKEN_CACHE_PATH, { encoding: 'utf8' });
return JSON.parse(data);
} catch (error) {
return null;
}
},
set: (accessToken, expires) => {
try {
const data = JSON.stringify({ accessToken, expires });
return fs.writeFileSync(TOKEN_CACHE_PATH, data);
} catch (e) {}
},
};
```
Then during the creation of the API object:
```typescript
const api = new Api({
workspace: "my-workspace",
config: "default",
api: "movies",
...,
authentication: {
type: "pkce",
clientId: "abcdefghijklmnopqrstuvwxyz",
callback: "https://my-domain.com/callback",
...,
tokenCache: fileSystemTokenCache
},
});
const query = {
endpoint: "movie",
parameters: { id: "https://imdb.com/data/movie/tt0209144" },
};
const response = await api.fetch(queries, { count: 10, offset: 0 });
```
Note: the Client ID and Callback URL cannot yet be configured from Spinque Desk.
Ask your system administrator to help you out.
### Utility functions

@@ -190,0 +218,0 @@

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

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc