From 5bfc694a53b5264cf3eb5bd3588fc947945733b3 Mon Sep 17 00:00:00 2001 From: Tim Hanssen Date: Mon, 22 Jan 2024 13:32:54 +0100 Subject: [PATCH 1/2] Update ReadMe and package --- README.md | 18 +++++++++--------- lib/index.d.ts | 6 +++--- lib/index.js | 25 ------------------------- package.json | 3 +-- 4 files changed, 13 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 3a416f5..0563850 100644 --- a/README.md +++ b/README.md @@ -28,8 +28,8 @@ const { createPreprClient } = require('@preprio/nodejs-sdk') const prepr = createPreprClient({ token: '', // required timeout: 4000, // default value - baseUrl: 'https://cdn.prepr.io', // default value (REST API), for GraphQL API use https://graphql.prepr.io/graphql - userId: null, // optional, used for AB testing + baseUrl: 'https://graphql.prepr.io/graphql', // default value (GraphQL API), for GraphQL API use https://cdn.prepr.io/ + customerId: null, // optional, used for AB testing and personalization }) module.exports = { prepr } @@ -46,10 +46,10 @@ To perform API requests you can make use of our fluent builder, this is how it l const { prepr } = require('./services/prepr') const result = await prepr - .graphqlQuery(`GraphQL Query`) // https://prepr.dev/docs/graphql/v1/collection-introduction + .graphqlQuery(`GraphQL Query`) // https://docs.prepr.io/reference/graphql/v1/fetching-collections .graphqlVariables({JSON_VARIABLE_PAYLOAD}) .timeout(8000) // Override globally set timeout for request cancellation - .userId('...') // Override globally set userId for ab testing + .customerId('...') // Override globally set userId for ab testing or personalization .token('xx-xx') // Update the Token used by the SDK for example when previewing staged content .fetch() // Fetch the collections ``` @@ -73,7 +73,7 @@ Let's request all Pages in our Prepr environment. .fetch() ``` -To help you querying our API we've added multiple examples to our [GraphQL Reference](https://prepr.dev/docs/graphql/v1/introduction). +To help you querying our API we've added multiple examples to our [GraphQL Reference](https://docs.prepr.io/reference/graphql/v1/fetching-collections). ## Usage REST API @@ -89,9 +89,9 @@ const result = await prepr 'model' : { 'eq' : 'x-x-x-x-x' } - }) // query data https://prepr.dev/docs/rest/v1/introduction - .timeout(8000) // Override globally set timeout for request cancellation - .userId('...') // Override globally set userId for ab testing + }) // query data https://docs.prepr.io/reference/rest/v1/introduction + .timeout(4000) // Override globally set timeout for request cancellation + .customerId('...') // Override globally set customerID for ab testing and personalization .token('xx-xx') // Update the Token used by the SDK for example when previewing staged content .sort('created_at') // Sort data .limit(8) // Limit the amount collections being returned @@ -99,7 +99,7 @@ const result = await prepr .fetch() // Fetch the collections ``` -To help you querying our API we've added multiple examples to our [REST Reference](https://prepr.dev/docs/rest/v1/introduction). +To help you to query our API we've added multiple examples to our [REST Reference](https://docs.prepr.io/reference/rest/v1/introduction). ## Reach out to us diff --git a/lib/index.d.ts b/lib/index.d.ts index 4bf8f94..5868960 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -3,7 +3,7 @@ declare module '@preprio/nodejs-sdk' { token?: string; baseUrl?: string; timeout?: number; - userId?: string; + customerId?: string; } class PreprClient { @@ -11,10 +11,10 @@ declare module '@preprio/nodejs-sdk' { token: string = null, baseUrl: string = 'https://cdn.prepr.io', timeout: number = 4000, - userId: string = null, + customerId: string = null, }: ClientOptions); - public userId(userId: string): this; + public customerId(customerId: string): this; public timeout(milliseconds: number): this; public query(query: Record): this; public sort(field: string): this; diff --git a/lib/index.js b/lib/index.js index 4f09610..529ec70 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,5 @@ const fetch = require('node-fetch') const AbortController = require('abort-controller') -const murmurhash = require('murmurhash') const qs = require('qs') const createPreprClient = options => new PreprClient(options) @@ -10,23 +9,13 @@ class PreprClient { token = null, baseUrl = 'https://cdn.prepr.io', timeout = 4000, - userId = null, customerId = null, }) { this._token = token this._baseUrl = baseUrl this._timeout = timeout this._path = null - this._hasAbTesting = false this._customerId = customerId - - userId && this._hashUserId(userId) - } - - userId(userId) { - this._hashUserId(userId) - - return this } customerId(customerId) { @@ -111,12 +100,6 @@ class PreprClient { Object.assign(headers, options.headers) - if (this._hasAbTesting) { - Object.assign(headers, { - 'Prepr-ABTesting': this._userId, - }) - } - if (this._customerId) { Object.assign(headers, { 'Prepr-Customer-Id': this._customerId, @@ -167,14 +150,6 @@ class PreprClient { } } - _hashUserId(userId) { - const hashValue = murmurhash.v3(userId, 1) - const ratio = hashValue / Math.pow(2, 32) - - this._userId = parseInt(ratio * 10000) - this._hasAbTesting = true - } - _queryStringBuilder({ query, sort, limit, skip }) { if (!query) { query = [] diff --git a/package.json b/package.json index 67d8b30..28473ea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@preprio/nodejs-sdk", - "version": "1.1.1", + "version": "2.0.0", "description": "The official Prepr SDK for Node.js", "main": "lib/index.js", "types": "lib/index.d.ts", @@ -25,7 +25,6 @@ "homepage": "https://github.com/preprio/nodejs-sdk#readme", "dependencies": { "abort-controller": "^3.0.0", - "murmurhash": "^2.0.0", "node-fetch": "^2.6.1", "qs": "^6.9.6" }, From fb237e93fa3465d4e3390bcc39037807749bb225 Mon Sep 17 00:00:00 2001 From: Tim Hanssen Date: Mon, 22 Jan 2024 13:37:36 +0100 Subject: [PATCH 2/2] linting --- README.md | 30 ++++++++++++++++-------------- lib/index.d.ts | 32 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 0563850..a36a14f 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ const { createPreprClient } = require('@preprio/nodejs-sdk') const prepr = createPreprClient({ token: '', // required timeout: 4000, // default value - baseUrl: 'https://graphql.prepr.io/graphql', // default value (GraphQL API), for GraphQL API use https://cdn.prepr.io/ + baseUrl: 'https://graphql.prepr.io/graphql', // default value (GraphQL API), for the REST API use https://cdn.prepr.io/ customerId: null, // optional, used for AB testing and personalization }) @@ -47,21 +47,22 @@ const { prepr } = require('./services/prepr') const result = await prepr .graphqlQuery(`GraphQL Query`) // https://docs.prepr.io/reference/graphql/v1/fetching-collections - .graphqlVariables({JSON_VARIABLE_PAYLOAD}) + .graphqlVariables({ JSON_VARIABLE_PAYLOAD }) .timeout(8000) // Override globally set timeout for request cancellation .customerId('...') // Override globally set userId for ab testing or personalization - .token('xx-xx') // Update the Token used by the SDK for example when previewing staged content + .token('xx-xx') // Update the Token used by the SDK for example when previewing staged content .fetch() // Fetch the collections ``` Let's request all Pages in our Prepr environment. ```js - // We created this earlier - const {prepr} = require('./services/prepr') - - const result = await prepr - .graphqlQuery(`{ +// We created this earlier +const { prepr } = require('./services/prepr') + +const result = await prepr + .graphqlQuery( + `{ Pages { items { _id @@ -69,8 +70,9 @@ Let's request all Pages in our Prepr environment. summary } } - }`) - .fetch() + }`, + ) + .fetch() ``` To help you querying our API we've added multiple examples to our [GraphQL Reference](https://docs.prepr.io/reference/graphql/v1/fetching-collections). @@ -86,9 +88,9 @@ const { prepr } = require('./services/prepr') const result = await prepr .path('/publications') // request path `https://cdn.prepr.io/publications` .query({ - 'model' : { - 'eq' : 'x-x-x-x-x' - } + model: { + eq: 'x-x-x-x-x', + }, }) // query data https://docs.prepr.io/reference/rest/v1/introduction .timeout(4000) // Override globally set timeout for request cancellation .customerId('...') // Override globally set customerID for ab testing and personalization @@ -106,5 +108,5 @@ To help you to query our API we've added multiple examples to our [REST Referenc You have questions about how to use this library or the Prepr API? Contact our support team at support@prepr.io or join us on [Slack](https://slack.prepr.io). -**You found a bug or want to propose a feature?**. +**You found a bug or want to propose a feature?**. File an issue here on GitHub. Don't share any authentication info in any code before sharing it. diff --git a/lib/index.d.ts b/lib/index.d.ts index 5868960..794712f 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,9 +1,9 @@ declare module '@preprio/nodejs-sdk' { interface ClientOptions { - token?: string; - baseUrl?: string; - timeout?: number; - customerId?: string; + token?: string + baseUrl?: string + timeout?: number + customerId?: string } class PreprClient { @@ -12,18 +12,18 @@ declare module '@preprio/nodejs-sdk' { baseUrl: string = 'https://cdn.prepr.io', timeout: number = 4000, customerId: string = null, - }: ClientOptions); + }: ClientOptions) - public customerId(customerId: string): this; - public timeout(milliseconds: number): this; - public query(query: Record): this; - public sort(field: string): this; - public limit(limit: number): this; - public skip(skip: number): this; - public path(path: string): this; - public graphqlQuery(graphQLQuery: string): this; - public graphqlVariables(graphQLVariables: Record): this; - public async fetch(options = {}): Promise>; + public customerId(customerId: string): this + public timeout(milliseconds: number): this + public query(query: Record): this + public sort(field: string): this + public limit(limit: number): this + public skip(skip: number): this + public path(path: string): this + public graphqlQuery(graphQLQuery: string): this + public graphqlVariables(graphQLVariables: Record): this + public async fetch(options = {}): Promise> } - export function createPreprClient(options: ClientOptions): PreprClient; + export function createPreprClient(options: ClientOptions): PreprClient }