|
| 1 | +# reso.js |
| 2 | + |
| 3 | +**`reso.js`** is a `Node.js` client for interacting with RESO Web API services, fully aligned with the current [RESO Web API specification](https://github.com/RESOStandards/transport). |
| 4 | + |
| 5 | +It includes features like custom request/response hooks, automatic authentication token refresh, and built-in rate limiting. |
| 6 | + |
| 7 | +## Table of Contents |
| 8 | + |
| 9 | +- [Installation](#installation) |
| 10 | +- [Compatibility](#compatibility) |
| 11 | +- [Usage](#usage) |
| 12 | + - [Getting Started](#getting-started) |
| 13 | + - [Bearer Token Authentication](#bearer-token-authentication) |
| 14 | + - [Client Credentials Authentication](#client-credentials-authentication) |
| 15 | + - [Querying the API](#querying-the-api) |
| 16 | + - [readByQuery](#readbyquery) |
| 17 | + - [readById](#readbyid) |
| 18 | + - [Fetching $metadata](#fetching-metadata) |
| 19 | + - [Rate Limiting](#rate-limiting) |
| 20 | + - [Hooks](#hooks) |
| 21 | + - [Error Handling](#error-handling) |
| 22 | +- [Contributing](#contributing) |
| 23 | +- [License](#license) |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +Install with your preferred package manager: |
| 28 | + |
| 29 | +```bash |
| 30 | +pnpm add reso.js |
| 31 | +# or |
| 32 | +npm install reso.js |
| 33 | +# or |
| 34 | +yarn add reso.js |
| 35 | +``` |
| 36 | + |
| 37 | +## Compatibility |
| 38 | + |
| 39 | +This library works with any RESO-compliant server following the [RESO Web API spec](https://github.com/RESOStandards/transport). Additionally, it provides official support for: |
| 40 | + |
| 41 | +- [Spark Platform](https://sparkplatform.com) |
| 42 | +- [Trestle](https://trestle.corelogic.com) |
| 43 | +- [Bridge Interactive](https://www.bridgeinteractive.com) |
| 44 | +- [MLS Grid](https://www.mlsgrid.com) |
| 45 | + |
| 46 | +## Usage |
| 47 | + |
| 48 | +### Getting Started |
| 49 | + |
| 50 | +`reso.js` supports both **Bearer Token** and **Client Credentials** authentication strategies. Refer to your feed's documentation for your feeds supported strategy and how to obtain credentials. |
| 51 | + |
| 52 | +#### Bearer Token Authentication |
| 53 | + |
| 54 | +```js |
| 55 | +import { createFeed } from 'reso.js'; |
| 56 | + |
| 57 | +const feed = createFeed({ |
| 58 | + http: { |
| 59 | + baseURL: 'http://my-reso-api.com', |
| 60 | + }, |
| 61 | + auth: { |
| 62 | + type: 'token', |
| 63 | + credentials: { |
| 64 | + token: 'MY_TOKEN', |
| 65 | + }, |
| 66 | + }, |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +> The bearer token provided must not expire. |
| 71 | + |
| 72 | +#### Client Credentials Authentication |
| 73 | + |
| 74 | +```js |
| 75 | +import { createFeed } from 'reso.js'; |
| 76 | + |
| 77 | +const feed = createFeed({ |
| 78 | + http: { |
| 79 | + baseURL: 'http://my-reso-api.com', |
| 80 | + }, |
| 81 | + auth: { |
| 82 | + type: 'credentials', |
| 83 | + credentials :{ |
| 84 | + tokenURL: 'http://my-reso-api.com/token' |
| 85 | + clientId: 'MY_CLIENT_ID' |
| 86 | + clientSecret: 'MY_CLIENT_SECRET' |
| 87 | + } |
| 88 | + }, |
| 89 | +}); |
| 90 | +``` |
| 91 | + |
| 92 | +You can optionally include `grantType` and `scope`. Tokens will automatically refresh 30 seconds before expiry by default. |
| 93 | + |
| 94 | +To custom the refresh buffer it can be passed in the with the auth strategy: |
| 95 | + |
| 96 | +```js |
| 97 | +import { createFeed } from 'reso.js'; |
| 98 | + |
| 99 | +const feed = createFeed({ |
| 100 | + http: { |
| 101 | + baseURL: 'http://my-reso-api.com', |
| 102 | + }, |
| 103 | + auth: { |
| 104 | + type: 'credentials', |
| 105 | + credentials :{ |
| 106 | + tokenURL: 'http://my-reso-api.com/token' |
| 107 | + clientId: 'MY_CLIENT_ID' |
| 108 | + clientSecret: 'MY_CLIENT_SECRET' |
| 109 | + }, |
| 110 | + refreshBuffer:40000, |
| 111 | + }, |
| 112 | +}); |
| 113 | +``` |
| 114 | + |
| 115 | +### Querying the API |
| 116 | + |
| 117 | +#### `readByQuery` |
| 118 | + |
| 119 | +Perform a RESO-compliant query. The function is an async generator that will auto paginate any next pages if the nextLink is present every time `.next()` is called or the result is looped over. |
| 120 | + |
| 121 | +```js |
| 122 | +for await (let properties of feed.readByQuery('Property', '$filter=ListPrice gt 100000')) { |
| 123 | + for (property of properties.values) { |
| 124 | + console.log(property); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +#### `readById` |
| 130 | + |
| 131 | +Retrieve a specific record by ID: |
| 132 | + |
| 133 | +```js |
| 134 | +const myProperty = await feed.readyById('Property', 123); |
| 135 | +console.log(myProperty.value); |
| 136 | +``` |
| 137 | + |
| 138 | +### Fetching `$metadata` |
| 139 | + |
| 140 | +Get the feed’s metadata XML: |
| 141 | + |
| 142 | +```js |
| 143 | +const metadata = await feed.$metadata(); |
| 144 | +console.log(metadata); |
| 145 | +``` |
| 146 | + |
| 147 | +### Rate Limiting |
| 148 | + |
| 149 | +Rate limiting is built-in for supported providers. You can also customize limits: |
| 150 | + |
| 151 | +```js |
| 152 | +const feed = createFeed({ |
| 153 | + http: { |
| 154 | + baseURL: 'http://my-reso-api.com', |
| 155 | + }, |
| 156 | + limiter: { |
| 157 | + duration: 30000, // 30 seconds |
| 158 | + points: 5, // max 5 requests per duration |
| 159 | + }, |
| 160 | +}); |
| 161 | +``` |
| 162 | + |
| 163 | +> `duration` defines the time window in milliseconds, and `points` sets the maximum number of requests allowed within that window. |
| 164 | + |
| 165 | +### Hooks |
| 166 | + |
| 167 | +The clients behaviour can be customized with the following supported hooks: |
| 168 | + |
| 169 | +- `onRequest` |
| 170 | +- `onRequestError` |
| 171 | +- `onResponse` |
| 172 | +- `onResponseError` |
| 173 | + |
| 174 | +These are backed by [ofetch interceptors](https://github.com/unjs/ofetch?tab=readme-ov-file#%EF%B8%8F-interceptors). Additional hooks will be added/supported as needed. |
| 175 | + |
| 176 | +**Example** – Appending `/replication` to `Property` requests: |
| 177 | + |
| 178 | +```js |
| 179 | +hooks: { |
| 180 | + onRequest: [ |
| 181 | + ({ request }) => { |
| 182 | + if (request.toLowerCase().includes('property')) { |
| 183 | + request += '/replication'; |
| 184 | + } |
| 185 | + }, |
| 186 | + ], |
| 187 | +}, |
| 188 | +``` |
| 189 | + |
| 190 | +### Error Handling |
| 191 | + |
| 192 | +The client throws a `FeedError` with detailed RESO error info: |
| 193 | + |
| 194 | +```js |
| 195 | +try { |
| 196 | + const myProperty = await feed.readById('Property', 123); |
| 197 | + console.log(myProperty.value); |
| 198 | +} catch (error) { |
| 199 | + if (error instanceof FeedError) { |
| 200 | + console.log(error.details); |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +## Contributing |
| 206 | + |
| 207 | +Bug reports, pull requests and feature discussions are welcome on [GitHub](https://github.com/ComfortablyCoding/reso.js) |
| 208 | + |
| 209 | +## License |
| 210 | + |
| 211 | +[MIT License](https://github.com/ComfortablyCoding/reso.js/blob/main/LICENSE) |
0 commit comments