Skip to content

Commit

Permalink
Feature/19 split browser and nodejs (#20)
Browse files Browse the repository at this point in the history
* #19 - remove NodeJS functionality

* #19 - remove not used packages

* #19 -validate fetch instance

* #19 - JSDocs fix

* #19 add more info to test logs

* #19 remove isBrowser helper, use a function. Remove unecesary defaults

* #19 add default serviceURL

* #19 remove isBrowser helper, use a function. Remove unecesary defaults

* #18 update jest setup

* #18 increase tests

* #18 update e2e tests

* #19 update docs

* #19 update docs

* #18 add test for persistent query variables

* #18 fix eslint for jest

* #19 update correct dev dependencies versions

* #19 limit packaged files

* #19 recreate package-lock

* #19 expose named export

* #19 fix workflow branch name

* #19 Readme update missing await

* #19 Add explicit async to async methods
  • Loading branch information
easingthemes committed Jun 4, 2021
1 parent 8fe9cbc commit 13510ea
Show file tree
Hide file tree
Showing 20 changed files with 3,103 additions and 2,017 deletions.
12 changes: 10 additions & 2 deletions .eslintrc.json
@@ -1,3 +1,11 @@
{
"extends": ["@adobe/eslint-config-aio-lib-config"]
}
"extends": ["@adobe/eslint-config-aio-lib-config"],
"rules": {
"jsdoc/check-tag-names": [
"error",
{
"definedTags": ["jest-environment"]
}
]
}
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches: [ '**' ]
pull_request:
branches: [ $default-branch ]
branches: [ main ]

jobs:
build:
Expand Down
2 changes: 0 additions & 2 deletions .npmrc

This file was deleted.

27 changes: 7 additions & 20 deletions README.md
Expand Up @@ -63,11 +63,11 @@ aemHeadlessClient.persistQuery(queryString, 'wknd/persist-query-name')
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))

aemHeadlessClient.runPersistedQuery('wknd/persist-query-name', { variable1: 'variable1Value' })
aemHeadlessClient.runPersistedQuery('wknd/persist-query-name')
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))

aemHeadlessClient.getQuery('wknd/persist-query-name-with-variables', { name: 'John Doe'})
aemHeadlessClient.runPersistedQuery('wknd/persist-query-name-with-variables', { name: 'John Doe'})
.then(data => console.log(data))
.catch(e => console.error(e.toJSON()))
```
Expand All @@ -76,27 +76,27 @@ aemHeadlessClient.getQuery('wknd/persist-query-name-with-variables', { name: 'Jo
(async () => {
let postData
try {
postData = aemHeadlessClient.postQuery(queryString)
postData = await aemHeadlessClient.postQuery(queryString)
} catch (e) {
console.error(e.toJSON())
}

let list
try {
list = aemHeadlessClient.listPersistedQueries()
list = await aemHeadlessClient.listPersistedQueries()
} catch (e) {
console.error(e.toJSON())
}

try {
aemHeadlessClient.persistQuery(queryString, 'wknd/persist-query-name')
await aemHeadlessClient.persistQuery(queryString, 'wknd/persist-query-name')
} catch (e) {
console.error(e.toJSON())
}

let getData
try {
getData = aemHeadlessClient.runPersistedQuery('wknd/persist-query-name')
getData = await aemHeadlessClient.runPersistedQuery('wknd/persist-query-name')
} catch (e) {
console.error(e.toJSON())
}
Expand All @@ -107,27 +107,14 @@ aemHeadlessClient.getQuery('wknd/persist-query-name-with-variables', { name: 'Jo

If `auth` param is a string, it's treated as a Bearer token

If `auth` param is an array, expected data is ['user', 'pass'] pair, and Basic Authorization will be ued
If `auth` param is an array, expected data is ['user', 'pass'] pair, and Basic Authorization will be used

If `auth` is not defined, Authorization header will not be set

### DEV token and service credentials

SDK contains helper function to get Auth token from credentials config file

```javascript
const { getToken } = require('@adobe/aem-headless-client-js')
(async () => {
const {accessToken, type, expires} = await getToken('path/to/service-config.json')
const sdkNode = new AEMHeadless('content/graphql/endpoint.gql', AEM_HOST_URI, accessToken)
const data = sdkNode.runQuery(queryString)
})()
```
## API Reference

See generated [API Reference](./api-reference.md)


## Contributing

Contributions are welcome! Read the [Contributing Guide](./.github/CONTRIBUTING.md) for more information.
Expand Down
118 changes: 89 additions & 29 deletions api-reference.md
Expand Up @@ -16,7 +16,7 @@ governing permissions and limitations under the License.
## AEMHeadless
This class provides methods to call AEM GraphQL APIs.
Before calling any method initialize the instance
with GraphQL endpoint, GraphQL host and auth if needed
with GraphQL endpoint, GraphQL serviceURL and auth if needed

**Kind**: global class

Expand All @@ -31,16 +31,34 @@ with GraphQL endpoint, GraphQL host and auth if needed

### new AEMHeadless(config)
Constructor.

If param is a string, it's treated as AEM server URL, default GraphQL endpoint is used.
For granular params, use config object


| Param | Type | Description |
| --- | --- | --- |
| config | <code>object</code> \| <code>string</code> | Configuration object, or AEM server URL string |
| [config.serviceURL] | <code>string</code> | AEM server URL |
| [config.endpoint] | <code>string</code> | GraphQL endpoint |
| [config.auth] | <code>string</code> \| <code>Array</code> | Bearer token string or [user,pass] pair array |
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>config</td><td><code>string</code> | <code>object</code></td><td><p>Configuration object, or AEM server URL string</p>
</td>
</tr><tr>
<td>[config.serviceURL]</td><td><code>string</code></td><td><p>AEM server URL</p>
</td>
</tr><tr>
<td>[config.endpoint]</td><td><code>string</code></td><td><p>GraphQL endpoint</p>
</td>
</tr><tr>
<td>[config.auth]</td><td><code>string</code> | <code>Array</code></td><td><p>Bearer token string or [user,pass] pair array</p>
</td>
</tr><tr>
<td>[config.fetch]</td><td><code>object</code></td><td><p>Fetch instance - required for NodeJS only, eg node-fetch/cross-fetch</p>
</td>
</tr> </tbody>
</table>

<a name="AEMHeadless+runQuery"></a>

Expand All @@ -49,11 +67,21 @@ Returns a Promise that resolves with a POST request JSON data.

**Kind**: instance method of [<code>AEMHeadless</code>](#AEMHeadless)
**Returns**: <code>Promise.&lt;any&gt;</code> - - the response body wrapped inside a Promise

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| query | <code>string</code> | | the query string |
| [options] | <code>object</code> | <code>{}</code> | additional POST request options |
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Default</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>query</td><td><code>string</code></td><td></td><td><p>the query string</p>
</td>
</tr><tr>
<td>[options]</td><td><code>object</code></td><td><code>{}</code></td><td><p>additional POST request options</p>
</td>
</tr> </tbody>
</table>

<a name="AEMHeadless+persistQuery"></a>

Expand All @@ -62,12 +90,24 @@ Returns a Promise that resolves with a PUT request JSON data.

**Kind**: instance method of [<code>AEMHeadless</code>](#AEMHeadless)
**Returns**: <code>Promise.&lt;any&gt;</code> - - the response body wrapped inside a Promise

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| query | <code>string</code> | | the query string |
| path | <code>string</code> | | AEM path to save query, format: configuration_name/endpoint_name |
| [options] | <code>object</code> | <code>{}</code> | additional PUT request options |
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Default</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>query</td><td><code>string</code></td><td></td><td><p>the query string</p>
</td>
</tr><tr>
<td>path</td><td><code>string</code></td><td></td><td><p>AEM path to save query, format: configuration_name/endpoint_name</p>
</td>
</tr><tr>
<td>[options]</td><td><code>object</code></td><td><code>{}</code></td><td><p>additional PUT request options</p>
</td>
</tr> </tbody>
</table>

<a name="AEMHeadless+listPersistedQueries"></a>

Expand All @@ -76,10 +116,18 @@ Returns a Promise that resolves with a GET request JSON data.

**Kind**: instance method of [<code>AEMHeadless</code>](#AEMHeadless)
**Returns**: <code>Promise.&lt;any&gt;</code> - - the response body wrapped inside a Promise

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | <code>object</code> | <code>{}</code> | additional GET request options |
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Default</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>[options]</td><td><code>object</code></td><td><code>{}</code></td><td><p>additional GET request options</p>
</td>
</tr> </tbody>
</table>

<a name="AEMHeadless+runPersistedQuery"></a>

Expand All @@ -88,10 +136,22 @@ Returns a Promise that resolves with a GET request JSON data.

**Kind**: instance method of [<code>AEMHeadless</code>](#AEMHeadless)
**Returns**: <code>Promise.&lt;any&gt;</code> - - the response body wrapped inside a Promise

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| path | <code>string</code> | | AEM path for persisted query, format: configuration_name/endpoint_name |
| [variables] | <code>object</code> | <code>{}</code> | query variables |
| [options] | <code>object</code> | <code>{}</code> | additional GET request options |
<table>
<thead>
<tr>
<th>Param</th><th>Type</th><th>Default</th><th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>path</td><td><code>string</code></td><td></td><td><p>AEM path for persisted query, format: configuration_name/endpoint_name</p>
</td>
</tr><tr>
<td>[variables]</td><td><code>object</code></td><td><code>{}</code></td><td><p>query variables</p>
</td>
</tr><tr>
<td>[options]</td><td><code>object</code></td><td><code>{}</code></td><td><p>additional GET request options</p>
</td>
</tr> </tbody>
</table>

13 changes: 6 additions & 7 deletions e2e/e2e.js
Expand Up @@ -10,11 +10,12 @@ governing permissions and limitations under the License.
*/

const path = require('path')
const fetch = require('cross-fetch')

// load .env values in the e2e folder, if any
require('dotenv').config({ path: path.join(__dirname, '.env') })

const { AEMHeadless } = require('../src')
const AEMHeadless = require('../src')

const { AEM_TOKEN, AEM_USER = 'admin', AEM_PASS = 'admin', AEM_HOST_URI = 'http://localhost:4502', AEM_GRAPHQL_ENDPOINT = 'content/graphql/global/endpoint.json' } = process.env

Expand All @@ -34,29 +35,27 @@ beforeEach(() => {
sdk = new AEMHeadless({
serviceURL: AEM_HOST_URI,
endpoint: AEM_GRAPHQL_ENDPOINT,
auth: AEM_TOKEN || [AEM_USER, AEM_PASS]
auth: AEM_TOKEN || [AEM_USER, AEM_PASS],
fetch
})
})

test('e2e test sdk valid params', () => {
// check success response
const config = { serviceURL: AEM_HOST_URI, auth: AEM_TOKEN || [AEM_USER, AEM_PASS] }
sdk = new AEMHeadless(config)
expect(sdk).toHaveProperty('serviceURL')
expect(sdk).toHaveProperty('endpoint')
})

test('e2e test sdk missing params', () => {
// check success response
const config = {}
const config = { fetch }
sdk = new AEMHeadless(config)
expect(sdk).toHaveProperty('serviceURL')
expect(sdk).toHaveProperty('endpoint')
})

test('e2e test sdk missing param serviceURL', () => {
// check success response
const config = { endpoint: 'test' }
const config = { endpoint: 'test', fetch }
sdk = new AEMHeadless(config)
expect(sdk).toHaveProperty('serviceURL')
expect(sdk).toHaveProperty('endpoint')
Expand Down
2 changes: 1 addition & 1 deletion e2e/jest.config.js
@@ -1,7 +1,7 @@
module.exports = {
testEnvironment: 'node',
setupFilesAfterEnv: [
'../test/jest/jest.setup.js'
'../test/setup/jest.setup.js'
],
testRegex: './e2e/e2e.js'
}

0 comments on commit 13510ea

Please sign in to comment.