Skip to content

Commit

Permalink
feat(node): new unified code snippet for both the webhook and metrics (
Browse files Browse the repository at this point in the history
…#879)

Redo the node code snippet for the webhook/metrics! We now have one code
snippet that handles both cases super easily.


```javascript
import { ReadMe } from 'readmeio'

const readme = ReadMe('API_KEY')

router.use(
  readme(async (req, getUser) => {
    // We pass in getUser which will call the correct function to fetch a 
    // user depending on what we have in the request
    const user = getUser({
      byEmail: getUserByEmail, // Function defined by them that returns a user given an email address
      byAPIKey: getUserByAPIKey, // Function defined by them that returns a user given an API key
      manualAPIKey: getAPIKey(req.headers.authorization), // Optional way to explicitly tell us the api key if we can't guess it
    });

    return {
      name: user.name,
      email: user.email,
      keys: user.apiKeys,
    };
  }, {
    development: process.env.NODE_ENV === 'development', // require development: true to show local testing page
  })
);
```

Other things:
* Added a /readme-setup page that walks users through making sure
everything is configured properly. This page is only enabled if
`development: true` to make sure that it won't be enabled in prod on
accident.
* Handles all of the metrics config in additional options parameter
(which is optional)
* Exported everything that was already there, so this is purely additive
* User provided function should return all of the information about the
user, even if it's not relevant for the specific API call that is
happening. This means we can have one set of config for the webhook and
metrics and the sdk will just figure out what is relevant for the call
to metrics.
* Makes a call to the ReadMe API to get the JWT Secret & stable version
(for linking to the dash)

### Update 1/31
* I renamed some of the exports to clean it up a bit. Now we just have a
`ReadMe` export that you call to pass in the API Key. Before we had a
separate `auth` export that was required to be called before the
middelware, which was confusing. This also cleans up how you call it (no
more readmeio.readme stuff). This matches Sentry's sdks closer which
makes me feel better about it.

## Testing
* Install `readmeio@next` to test the new snippet
* Docs on setup here:
https://docs.readme.com/main/docs/unified-snippet-docs
* PR for readme's api here:
readmeio/readme#10819

Things to look out for:
* Do the docs make sense? 
* Issues with copy in the local setup page?
* Testing various APIs (with various auth types)

---------

Co-authored-by: Tony Li <[email protected]>
Co-authored-by: Kanad Gupta <[email protected]>
Co-authored-by: Kanad Gupta <[email protected]>
Co-authored-by: Jon Ursenbach <[email protected]>
Co-authored-by: Bill Mill <[email protected]>
Co-authored-by: Aaron Hedges <[email protected]>
Co-authored-by: James Clark <[email protected]>
Co-authored-by: James Clark <[email protected]>
  • Loading branch information
9 people authored Feb 6, 2024
1 parent c442510 commit 326e953
Show file tree
Hide file tree
Showing 20 changed files with 16,984 additions and 210 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
packages/cloudflare-worker/coverage/
packages/cloudflare-worker/dist/
packages/node/src/.api/
packages/node/coverage/
packages/node/dist/
packages/php/examples/
Expand Down
1,238 changes: 1,035 additions & 203 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"name": "metrics-sdks-monorepo",
"private": true,
"scripts": {
"clean": "npx lerna clean",
"clean": "npx lerna@6 clean",
"lint": "eslint test/ && npm run lint --workspaces",
"lint:docs": "alex .",
"prettier": "prettier --list-different \"./**/**.{js,ts,md}\"",
"prettier:write": "prettier --list-different --write \"./**/**.{js,ts,md}\"",
"publish": "npx lerna publish",
"publish": "npx lerna@6 publish",
"test": "npm test --workspaces",
"test:integration-metrics": "vitest test/integration-metrics.test.js",
"test:integration-metrics-watch": "vitest run test/integration-metrics.test.js",
Expand Down
5 changes: 4 additions & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "readmeio",
"description": "Track your API metrics within ReadMe.",
"version": "6.2.0",
"version": "7.0.0-alpha.5",
"license": "ISC",
"homepage": "https://readme.com/metrics",
"bugs": {
Expand All @@ -27,11 +27,14 @@
"dependencies": {
"@types/har-format": "^1.2.10",
"@types/node-fetch": "^2.6.2",
"api": "^6.1.1",
"content-type": "^1.0.5",
"find-cache-dir": "^3.3.2",
"flat-cache": "^3.0.4",
"json-schema-to-ts": "^2.9.2",
"lodash": "^4.17.15",
"node-fetch": "^2.6.7",
"oas": "^20.10.2",
"ssri": "^10.0.1",
"timeout-signal": "^1.1.0",
"type-is": "^1.6.18",
Expand Down
11 changes: 11 additions & 0 deletions packages/node/src/.api/api.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "1.0",
"apis": [
{
"identifier": "developers",
"source": "@developers/v2.0#5a1mg1plldzmnq5",
"integrity": "sha512-pA1uoUA9nuzjAXOzYpXzBxL8P9YgJbthrDnufYNJS6YJnK0j0Rz5wNA3JlwJrA69vJpDTu22MB4JnSlifeE0oA==",
"installerVersion": "6.1.1"
}
]
}
Loading

0 comments on commit 326e953

Please sign in to comment.