Skip to content

Commit aa32198

Browse files
authored
Merge pull request #25 from NYPL/scc-4022-node20
SCC-4022 : Node20
2 parents 172546a + 81bbddc commit aa32198

21 files changed

Lines changed: 8477 additions & 2622 deletions

.env.example renamed to .env-example

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
AWS_ACCESS_KEY_ID=[snip]
2-
AWS_SECRET_ACCESS_KEY=[snip]
31
NYPL_API_BASE_URL=[https://url-to-our-playform.example.com/api/v0.1/]
42
LOG_LEVEL=error
53
NYPL_OAUTH_KEY=[an-oauth-key]

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
node_modules
33

44
# OAUTH keys & other vars:
5-
.env
5+
.env*
6+
# Allow the example file:
7+
!.env-example

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.17.1
1+
20

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
language: node_js
22
install: npm install
33
script: npm test
4+
dist: jammy

README.md

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# NYPL Data API Client
22

3-
Helper lib for interacting with the (internal) NYPL Data API
3+
Node module for interacting with the NYPL Platform API
44

55
## Installation
66

7-
Install it via npm for use inside your project:
8-
97
```js
108
npm i @nypl/nypl-data-api-client --save
119
```
@@ -35,106 +33,98 @@ See [usage.md](usage.md) for complete documentation of Client methods and use.
3533
To authenticate and fetch a bib (all GETs authenticate first, by default):
3634

3735
```js
38-
client.get('bibs/sierra-nypl/17746307').then((resp) => {
39-
let bib = resp.data
40-
console.log('Got bib: ' + bib.title)
41-
}).catch((e) => console.error('Error authenticating or fetching bib: ', e))
36+
try {
37+
const resp = await client.get('bibs/sierra-nypl/17746307')
38+
const bib = resp.data
39+
console.log('Got bib: ' + bib.title)
40+
} catch (e) => {
41+
console.error('Error authenticating or fetching bib: ', e)
42+
}
4243
```
4344

4445
To get the "Item" stream schema, which doesn't require authentication:
4546
```js
46-
client.get('current-schemas/Item', { authenticate: false }).then((resp) => {
47-
let schema = resp.data
47+
const resp = await client.get('current-schemas/Item', { authenticate: false })
48+
const schema = resp.data
4849
// Now we can build an avro encoder by parsing the escaped "schema" prop:
49-
var avroType = require('avsc').parse(JSON.parse(schema.schema))
50-
})
50+
const avroType = require('avsc').parse(JSON.parse(schema.schema))
5151
```
5252

5353
To get patron id `12345678` (note you must auth with an account that has the 'read:patron' role):
5454
```js
55-
client.get('patrons/12345678').then((resp) => {
56-
let patron = resp.data
57-
if (!patron) console.error('Could not find patron')
58-
else {
59-
var pType = Object.keys(patron.fixedFields).map((key) => patron.fixedFields[key])
60-
.filter((fixed) => fixed.label === 'Patron Type')[0]
61-
.value
62-
var name = patron.names[0]
63-
var barcode = patron.barCodes[0]
64-
65-
console.log('Patron:')
66-
console.log(' Name: ' + name)
67-
console.log(' Barcode: ' + barcode)
68-
console.log(' Patron Type: ' + pType)
69-
}
70-
})
55+
const resp = await client.get('patrons/12345678')
56+
const patron = resp.data
57+
if (!patron) throw new Error('Could not find patron')
58+
59+
const pType = Object.keys(patron.fixedFields).map((key) => patron.fixedFields[key])
60+
.filter((fixed) => fixed.label === 'Patron Type')[0]
61+
.value
62+
var name = patron.names[0]
63+
var barcode = patron.barCodes[0]
64+
65+
console.log('Patron:')
66+
console.log(' Name: ' + name)
67+
console.log(' Barcode: ' + barcode)
68+
console.log(' Patron Type: ' + pType)
7169
```
7270

7371
To POST a new "TestSchema" schema:
7472
```js
75-
client.post('schemas/TestSchema', { name: "TestSchema", type: "record", fields: [ ... ] })
76-
.then((resp) => {
77-
if (JSON.parse(resp).data.stream !== 'TestSchema') throw Error('Error creating schema...')
78-
})
73+
const resp = await client.post('schemas/TestSchema', { name: "TestSchema", type: "record", fields: [ ... ] })
74+
if (resp.data.stream !== 'TestSchema') throw Error('Error creating schema...')
7975
```
8076

8177
## CLI
8278

8379
A small CLI exists for common tasks.
8480

85-
If installed globally (i.e. `npm i -g @nypl/nypl-data-api-client`), it can be run as follows:
81+
Set up:
8682

87-
```js
88-
nypl-data-api
83+
```
84+
git clone git@github.com:NYPL/node-nypl-data-api-client.git
85+
cd node-nypl-data-api-client
86+
nvm use; npm i
87+
88+
cp .env-example .env
89+
# Fill in missing details in .env
8990
```
9091

91-
For local installs, it can be run via local `node_modules`:
92+
To fetch the first 25 bibs:
9293

9394
```js
94-
./node_modules/.bin/nypl-data-api
95+
./bin/nypl-data-api.js get bibs
9596
```
9697

97-
To get help with any command run:
98+
To fetch a specific bib:
9899

99100
```js
100-
nypl-data-api help [command]
101+
./bin/nypl-data-api.js get bibs/sierra-nypl/11040445
101102
```
102103

103-
Note that the cli uses the following environment variables, read by default from `.env`:
104+
To create a hold-request:
104105

105-
- NYPL_API_BASE_URL
106-
- NYPL_OAUTH_KEY
107-
- NYPL_OAUTH_SECRET
108-
- NYPL_OAUTH_URL
109-
110-
See `.env.example` for a sample `.env` file. To specify a different `.env`, use the `--envfile` param (e.g. `--envfile config/qa.env`)
106+
```js
107+
./bin/nypl-data-api.js post hold-requests '{ "record": "1234", "patron": ... }'
108+
```
111109

112-
### Schema post
110+
To specify a different `.env`, use the `--envfile` param (e.g. `--envfile .env-qa`)
113111

114-
Run the following to upload the content of the given jsonfile to `schemas/[name]`
112+
A special `schema` command is provided for updating schemas. To post a new schema, run the following to upload the content of the given jsonfile to `schemas/[name]`
115113

116114
```js
117-
nypl-data-api schema post [name] [jsonfile]
115+
./bin/nypl-data-api.js schema post [name] [jsonfile]
118116
```
119117

120118
## Contributing
121119

122-
1. Cut feature branch from `master`
123-
2. After review, merge to `master`
120+
1. Cut feature branchs from `main`
121+
2. After review, merge to `main`
124122
3. Bump version in `package.json` & note changes in `CHANGELOG.md`
125123
4. `git tag -a v[version]`
126124
5. `npm publish`
127125

128126
## Testing
129127

130-
All tests work offline with `request` and `oauth` stubs:
131-
132128
```js
133129
npm test
134130
```
135-
136-
If you want to run the test suite against real infrastructure, you can do so as follows:
137-
138-
```js
139-
USE_CREDENTIALS=[credentials file, e.g. '.env'] npm test
140-
```

0 commit comments

Comments
 (0)