@readme/metrics-sdk-snippets
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

@readme/metrics-sdk-snippets

Generate code snippets for ReadMe Metrics SDKs in multiple languages.

Build

This library was built for ReadMe's Metrics onboarding flow.

Installation

npm install --save @readme/metrics-sdk-snippets

Usage

import { MetricsSDKSnippet } from '@readme/metrics-sdk-snippets';

const { convert } = new MetricsSDKSnippet([
  {
    name: 'petstore_auth',
    default: 'default-key',
    source: 'security',
    type: 'oauth2',
  },
  {
    name: 'basic_auth',
    default: 'default',
    source: 'security',
    type: 'http',
  },
]);

console.log(convert('webhooks', 'node', 'express'));

This generates the following object:

{
  ranges: {
    "security": {
      "petstore_auth": { "line": 26 }, // The line where this data is at.
      "basic_auth": { "line": 27 }
    }
  },
  snippet: '// see below',
}

The generated snippet for this results in a ReadMe Node Metrics SDK webhooks example:

// Save this code as `server.js`
// Run the server with `node server.js`
const readme = require('readmeio');
const express = require('express');

const app = express();

app.post('/webhook', express.raw({ type: 'application/json' }), (req, res) => {
  // Verify the request is legitimate and came from ReadMe
  const signature = req.headers['readme-signature'];

  // Your ReadMe secret
  const secret = 'rdme_xxxx';

  try {
    readme.verify(req.body, signature, secret);
  } catch (e) {
    // Handle invalid requests
    return res.sendStatus(401);
  }

  // Fetch the user from the db
  db.find({ email: req.body.email }).then(user => {
    return res.json({
      // OAS Security variables
      petstore_auth: 'default-key',
      basic_auth: { user: 'user', pass: 'pass' },
    });
  });
});

Readme

Keywords

Package Sidebar

Install

npm i @readme/metrics-sdk-snippets@1.0.0

Version

1.0.0

License

ISC

Unpacked Size

41.7 kB

Total Files

40

Last publish

Collaborators

  • gkoberger
  • domharrington
  • mjcuva
  • kanadgupta
  • jonursenbach
  • rafegoldberg
  • dannobytes
  • gratcliff
  • llimllib
  • darrenyong
  • azinder1
  • kellyjosephprice