Skip to content

Commit

Permalink
remove batch API
Browse files Browse the repository at this point in the history
– 150 req/m limit applies to each batch item
– it's easier to manage individual responses
– simplify the lib scope, and remove extra namespace
  • Loading branch information
alekseykulikov committed Jun 7, 2021
1 parent b04024c commit c4adbcb
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 501 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
dist
result
node_modules
51 changes: 2 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# crux-api

> A [Chrome UX Report API](https://developers.google.com/web/tools/chrome-user-experience-report/api/reference) wrapper that supports batching, handles errors, and provides types.
> A [Chrome UX Report API](https://developers.google.com/web/tools/chrome-user-experience-report/api/reference) wrapper that handles errors, retries, and provides types.
**Motivation**: [CrUX API](https://web.dev/chrome-ux-report-api/) is a fantastic tool to get RUM data without installing any script.
While using the API in [Treo](https://treo.sh/), we discovered a few complex cases like API errors, rate limits, not found entries, a complicated multipart response from the batch API, URLs normalization, and TypeScript notations. We decided to build the `crux-api` library to makes it easier to work with the CrUX API.

**Features**:

- A tiny (450 bytes) wrapper for [Chrome UX Report API](https://developers.google.com/web/tools/chrome-user-experience-report/api/reference);
- [Batch API support](https://developers.google.com/web/tools/chrome-user-experience-report/api/guides/batch) for up to 1000 records per one request;
- TypeScript notations for [options and responses](https://developers.google.com/web/tools/chrome-user-experience-report/api/reference/rest/v1/records/queryRecord);
- Isomorphic: works in a browser and node.js;
- Returns `null` for the `404 (CrUX data not found)` response;
Expand All @@ -33,20 +32,6 @@ const res1 = await queryRecord({ url: 'https://www.github.com/' }) // fetch all
const res2 = await queryRecord({ url: 'https://www.github.com/explore', formFactor: 'DESKTOP' }) // fetch data for desktop devices
```

Use the [CrUX Batch API](https://developers.google.com/web/tools/chrome-user-experience-report/api/guides/batch) to combine up to 1000 requests and get results in less than 1 second:

```js
import { createBatch } from 'crux-api/batch'
const batch = createBatch({ key: CRUX_API_KEY })

const records = await batch([
{ url: 'https://github.com/', formFactor: 'MOBILE', effectiveConnectionType: '4G' },
{ url: 'https://github.com/marketplace', formFactor: 'DESKTOP' },
{ url: 'https://www.github.com/explore', formFactor: 'TABLET' },
// ... up to 1000 records.
])
```

Fetch origin-level data in node.js using [`node-fetch`](https://www.npmjs.com/package/node-fetch):

```js
Expand Down Expand Up @@ -97,9 +82,7 @@ Result is `null` or an `object` with [queryRecord response body](https://develop

## API

### Single Record Request

#### createQueryRecord(createOptions)
### createQueryRecord(createOptions)

Returns a `queryRecord` function.

Expand Down Expand Up @@ -129,36 +112,6 @@ const res = await queryRecord({
// res -> URL-level data for https://github.com/marketplace
```

### Batch Request

`crux-api/batch` uses the [CrUX Batch API](https://developers.google.com/web/tools/chrome-user-experience-report/api/guides/batch), which allows combining 1000 calls in a single batch request. It's a separate namespace because the API is different, and it's bigger (850 bytes) due to the complexity of constructing and parsing multipart requests.

_Note_: A set of `n` requests batched together counts toward your usage limit as `n` requests, not as one request. That's why the sometimes a batch response contains `429` responses. But the `crux-api` automatically retries these responses, aiming always to return the data you need.

#### createBatch(createOptions)

Accepts the same [`createOptions` as the `createQueryRecord`](#createqueryrecordcreateoptions) and returns a `batch` function.

#### batch(batchOptions)

Accepts an array of [`queryRecord`](#queryrecordqueryoptions) options and returns an array with an exact position for each record. If the record doesn't exist in CrUX index, the value set to null. If some requests hit rate-limit, `batch` will retry them after a short timeout.

```js
import { createBatch } from 'crux-api/batch'
import nodeFetch from 'node-fetch'

const batch = createBatch({ key: process.env.CRUX_KEY, fetch: nodeFetch })
const res = await batch([
{ origin: 'https://example.com' },
{ url: 'https://github.com/', formFactor: 'DESKTOP' },
{ origin: 'https://fooo.bar' },
])

// res[0] -> origin-level data for https://example.com
// res[1] -> URL-level data for https://github.com/ on desktop devices
// res[2] -> null (invalid origin that not found in the CrUX index)
```

### normalizeUrl(url)

Normalize a URL to match the CrUX API internal index.
Expand Down
11 changes: 0 additions & 11 deletions batch/package.json

This file was deleted.

261 changes: 0 additions & 261 deletions batch/src/index.js

This file was deleted.

Loading

0 comments on commit c4adbcb

Please sign in to comment.