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
46 changes: 24 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const { createPreprClient } = require('@preprio/nodejs-sdk')
const prepr = createPreprClient({
token: '<your access 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 the REST API use https://cdn.prepr.io/
customerId: null, // optional, used for AB testing and personalization
})

module.exports = { prepr }
Expand All @@ -46,34 +46,36 @@ 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
.graphqlVariables({JSON_VARIABLE_PAYLOAD})
.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
.token('xx-xx') // Update the Token used by the SDK for example when previewing staged content
.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
```

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
title
summary
}
}
}`)
.fetch()
}`,
)
.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

Expand All @@ -86,25 +88,25 @@ 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'
}
}) // 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
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
.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
.skip(0) // Skip the first x amount collections before returning resources
.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

You have questions about how to use this library or the Prepr API?
Contact our support team at [email protected] 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.
34 changes: 17 additions & 17 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
declare module '@preprio/nodejs-sdk' {
interface ClientOptions {
token?: string;
baseUrl?: string;
timeout?: number;
userId?: string;
token?: string
baseUrl?: string
timeout?: number
customerId?: string
}

class PreprClient {
public constructor({
token: string = null,
baseUrl: string = 'https://cdn.prepr.io',
timeout: number = 4000,
userId: string = null,
}: ClientOptions);
customerId: string = null,
}: ClientOptions)

public userId(userId: string): this;
public timeout(milliseconds: number): this;
public query(query: Record<string, any>): 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<string, any>): this;
public async fetch(options = {}): Promise<Record<string, any>>;
public customerId(customerId: string): this
public timeout(milliseconds: number): this
public query(query: Record<string, any>): 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<string, any>): this
public async fetch(options = {}): Promise<Record<string, any>>
}
export function createPreprClient(options: ClientOptions): PreprClient;
export function createPreprClient(options: ClientOptions): PreprClient
}
25 changes: 0 additions & 25 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = []
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
},
Expand Down