Skip to content

Commit ff80edd

Browse files
committed
chore: create example project using API
fix: fetch dependency usage
1 parent b4f4dd6 commit ff80edd

7 files changed

Lines changed: 64 additions & 2 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ package-lock.json
33
.vscode
44

55
*-cache.json
6-
scripts/
6+
scripts/
7+
8+
.env

example/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ENTERPRISE_API_KEY = 'ent_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
2+
SETSTORE_PLUGIN_API_KEY = 'ss_live_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
3+
SETSTORE_CONNECTION_API_KEY = 'sc_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

example/index.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { MCSetsAPI } from 'mcsets-js-sdk-client'
2+
import dotenv from 'dotenv'
3+
4+
// Inject ENV vars from ENV file
5+
dotenv.config({quiet: true})
6+
7+
// Create constants with API keys from ENV vars
8+
const enterpriseApiKey = process.env.ENTERPRISE_API_KEY
9+
const setStorePluginApiKey = process.env.SETSTORE_PLUGIN_API_KEY
10+
const setStoreConnectionApiKey = process.env.SETSTORE_CONNECTION_API_KEY
11+
12+
// Base API required no auth so we will set the key to null
13+
const baseApi = MCSetsAPI.from(null, 'base')
14+
15+
const enterpriseApi = MCSetsAPI.from(enterpriseApiKey, 'enterprise')
16+
17+
// Create two set store API instances with both API keys to support both auth methods
18+
const setStorePluginApi = MCSetsAPI.from(setStorePluginApiKey, 'setstore')
19+
const setStoreConnectionApi = MCSetsAPI.from(setStoreConnectionApiKey, 'setstore')
20+
21+
/**
22+
* Example API calls are made below
23+
*/
24+
25+
// Use base API to fetch health status (no auth required)
26+
await baseApi.getHealth().then(console.log)
27+
28+
// Use the enterprise API to fetch products (requires enterprise API key, see ENV for key examples)
29+
await enterpriseApi.getProducts().then(console.log)
30+
31+
// Use the set store plugin API to fetch pending delivery commands (requires set store plugin API key, see ENV for key examples)
32+
await setStorePluginApi.fetchPendingDeliveryCommands().then(console.log)

example/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "An example project using the MCSets JS SDK client.",
5+
"license": "MIT",
6+
"author": "Ian Tapply",
7+
"type": "module",
8+
"main": "index.js",
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1"
11+
},
12+
"dependencies": {
13+
"dotenv": "^17.3.1",
14+
"mcsets-js-sdk-client": "file:../"
15+
}
16+
}

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,6 @@ declare module 'mcsets-js-sdk-client' {
144144
total: number
145145
}
146146
}
147+
148+
export default MCSetsAPI
147149
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "1.0.2",
44
"description": "Library to manage and interact with MCSets API's",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
78
"test": "mocha --recursive --reporter spec --exit",
89
"pretest": "npm run lint",

src/rest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
const debug = require('debug')('mcsets-js-sdk-client')
2-
const fetch = require('node-fetch')
2+
3+
let fetch
4+
try {
5+
fetch = globalThis.fetch || require('node-fetch')
6+
} catch (e) {
7+
throw new Error('Fetch API is not available. Please ensure you are running in an environment with fetch support or install node-fetch for Node.js environments.')
8+
}
39

410
const constants = require('./constants')
511

0 commit comments

Comments
 (0)