# dronedeploy-functions-api

> Public GraphQL API and Wrapper for Dronedeploy Functions

Latest version **1.1.1** (published 2018-07-09) · MIT License license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install dronedeploy-functions-api
pnpm add dronedeploy-functions-api
yarn add dronedeploy-functions-api
bun add dronedeploy-functions-api
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2018-07-09 |
| First published | 2018-05-17 |
| Weekly downloads | 0 |
| License | MIT License |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 39.3 KB |
| Known vulnerabilities | 0 (+6 in 3 direct dependencies) |
| Install scripts | no |
| Maintainers | dronedeploy-ops, dronedeploy-user |

## Links

- npm: https://www.npmjs.com/package/dronedeploy-functions-api
- npm.io page: https://npm.io/package/dronedeploy-functions-api

## Dependencies (4)

- [pem-jwk](https://npm.io/package/pem-jwk.md) ~1.5.1
- [request](https://npm.io/package/request.md) 2.85.0
- [node-fetch](https://npm.io/package/node-fetch.md) 2.1.2
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ~8.2.1

## Recent versions

- 1.1.1 (latest) — 2018-07-09
- 1.1.0 — 2018-06-07
- 1.0.3 — 2018-05-18
- 1.0.2 — 2018-05-18
- 1.0.1 — 2018-05-18
- 1.0.0 — 2018-05-17

## README

Drondeploy Functions API
========================

Installation
============

Until this repo is published on NPM
Install via
`npm install --save git+ssh://git@github.com/dronedeploy/dronedeploy-functions-api.git`


How to use this repo in your DroneDeploy function
==========

In your index.js

Import the module like so
```
const bootstrap = require('dronedeploy-functions-api');
```

The bootstrap method handles
- Authentication
- Default HTTP responses for auth failure, and OPTIONS routes
- Setting up CORS Headers

The bootstrap method returns an error or a ctx object.
If an error is present a response will already have been sent ( at least in this version ), so do not use the `res` objects methods if error is received.

The ctx object will have all the api methods available
and references to the jwt token in both raw and decrypted form.

```
bootstrap(config, req, res, (err, ctx) => {
  if (err) {
    console.error(err, err.stack);
    console.warn('An error occurred during the bootstrapping process. A default response has been sent and code paths have been stopped.');
    return;
  }
});
```

The recommended usage (this and the above will likely be handled by dronedeploy-cli automatically ) is to import a file named handler.js, where most of the developers code lives.
handler.js should expose one public method

```
// handler.js
module.exports = (req, res, ctx) => {

}
```

Temporarily for testing purposes you can override the `ctx.originalToken` here if you set `mockToken: true `
and `authRequired: false`
in the config object. This is subject to change at any time.


Api Methods
================

table(tableId)

Methods on the table object

- addRow(externalKey, data)
- editRow(externalKey, data)
- upsertRow(externalKey, data) // subject to change

Example Response for add, edit, upsert

```
{ ok: true,
  data:
   { id: 'TableData:5ae711f72852b900016b0895',
     application: { id: 'Application:lonvecnbfyvovfqsvbxz' },
     data: { name: 'Michael Hernandez' },
     externalKey: 'mhernandez+test@dronedeploy.com-user-information',
     table: { id: 'Table:5ada2d8f27b7b90001b9c40a' }
   }
 }
```
- getRow(externalKey)

Example Response for get

```
{ ok: true, data: { name: 'Michael Hernandez' } }
```

Example handler showing the aformentioned methods in use, also see file `examples/basic.js`

```
function handler(req, res, ctx) {
  // this is for mocking token.
  ctx.originalToken = process.argv[2] // or switch this out for yours.;

  // Get the users tables
  let users = ctx
    .datastore
    .table('Table:5ada2d8f27b7b90001b9c40a');

  users
    // .addRow('mhernandez+test@dronedeploy.com', {name: 'Michaxel Hernandez'})
    // .editRow('mhernandez+test@dronedeploy.com', {name: 'Michaxel Hernandez'})
    .upsertRow('mhernandez+test@dronedeploy.com', {name: 'Michaxel Hernandez'})
    .then(result => {
      console.log(util.inspect(result, {depth: 20, colors: true}));
    })
    .then(() => {
      users
        .getRowByExternalId('mhernandez+test@dronedeploy.com')
        .then((result) => {
          console.log(util.inspect(result, {depth: 20, colors: true}));
        })

    })
    .catch(e => {
      console.log(e);
    });
}

```

Configuration
================
example configuration object

```
// in the index.js
config = require('./config.json');
// may change to yaml
boostrap(config, req, res, (err, ctx) => {})

// config.json
{
  "authRequired": false,  // set to true for testing or if your function does not use dd's api's
  "mockToken": true, // set to true to allow token
  "cors": {
    "headers": { // add custom headers that should be allowed past cors here
      'x-custom-token': 'KFKKAJFOO@#J!@#MO'
    }
  }
}
```

---
_Source: https://npm.io/package/dronedeploy-functions-api · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
