All Projects → netlify → Js Client

netlify / Js Client

A Open-API derived JS + Node.js API client for Netlify

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Js Client

Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1928.24%)
Mutual labels:  api, rest, swagger, openapi
Mobx Rest
REST conventions for Mobx
Stars: ✭ 164 (-3.53%)
Mutual labels:  api, rest, api-client, api-rest
Hapi Openapi
Build design-driven apis with OpenAPI (formerly swagger) 2.0 and hapi.
Stars: ✭ 196 (+15.29%)
Mutual labels:  api, rest, swagger, openapi
Proteus
Lean, mean, and incredibly fast JVM framework for web and microservice development.
Stars: ✭ 178 (+4.71%)
Mutual labels:  api, rest, swagger, openapi
Compojure Api
Sweet web apis with Compojure & Swagger
Stars: ✭ 1,056 (+521.18%)
Mutual labels:  api, rest, swagger, openapi
Goa
Design-based APIs and microservices in Go
Stars: ✭ 4,493 (+2542.94%)
Mutual labels:  api, rest, swagger, openapi
Loopback Next
LoopBack makes it easy to build modern API applications that require complex integrations.
Stars: ✭ 3,972 (+2236.47%)
Mutual labels:  api, rest, swagger, openapi
Angular Swagger Ui
An angularJS implementation of Swagger UI
Stars: ✭ 131 (-22.94%)
Mutual labels:  api, swagger, openapi, api-rest
Gemini
Model Driven REST framework to automatically generate CRUD APIs
Stars: ✭ 138 (-18.82%)
Mutual labels:  rest, swagger, openapi, api-rest
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+23187.06%)
Mutual labels:  api, rest, swagger, openapi
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+6155.29%)
Mutual labels:  api, rest, openapi, api-client
Flama
🔥 Fire up your API with this flamethrower
Stars: ✭ 161 (-5.29%)
Mutual labels:  api, rest, swagger, openapi
Cats
Generate tests at runtime based on OpenApi specs
Stars: ✭ 86 (-49.41%)
Mutual labels:  api, swagger, openapi
Openapi Viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 82 (-51.76%)
Mutual labels:  api, swagger, openapi
Api Client Generator
Angular REST API client generator from Swagger YAML or JSON file with camel case settigs
Stars: ✭ 92 (-45.88%)
Mutual labels:  api, rest, swagger
Kaizen Openapi Editor
Eclipse Editor for the Swagger-OpenAPI Description Language
Stars: ✭ 97 (-42.94%)
Mutual labels:  rest, swagger, openapi
Dreamfactory
DreamFactory API Management Platform
Stars: ✭ 1,148 (+575.29%)
Mutual labels:  api, rest, openapi
Appy Backend
A user system to bootstrap your app.
Stars: ✭ 96 (-43.53%)
Mutual labels:  api, rest, swagger
Swagger Combine
Combines multiple Swagger schemas into one dereferenced schema.
Stars: ✭ 102 (-40%)
Mutual labels:  api, swagger, openapi
Openapi Spring Webflux Validator
🌱 A friendly kotlin library to validate API endpoints using an OpenApi 3.0 and Swagger 2.0 specification
Stars: ✭ 67 (-60.59%)
Mutual labels:  rest, swagger, openapi

netlify/js-client

npm version build status coverage dependencies downloads

A Netlify OpenAPI client that works in the browser and Node.js.

Usage

const NetlifyAPI = require('netlify')

const listNetlifySites = async function () {
  const client = new NetlifyAPI('1234myAccessToken')
  const sites = await client.listSites()
  return sites
}

Using OpenAPI operations

const NetlifyAPI = require('netlify')

const client = new NetlifyAPI('1234myAccessToken')

const listCreateAndDeleteSite = async function () {
  // Fetch sites
  const sites = await client.listSites()

  // Create a site. Notice `body` here for sending OpenAPI body
  const site = await client.createSite({
    body: {
      name: `my-awesome-site`,
      // ... https://open-api.netlify.com/#/default/createSite
    },
  })

  // Delete site. Notice `site_id` is a path parameter https://open-api.netlify.com/#/default/deleteSite
  await client.deleteSite({
    site_id: siteId,
  })
}

API

client = new NetlifyAPI([accessToken], [opts])

Create a new instance of the Netlify API client with the provided accessToken.

accessToken is optional. Without it, you can't make authorized requests.

opts includes:

const opts = {
  userAgent: 'netlify/js-client',
  scheme: 'https',
  host: 'api.netlify.com',
  pathPrefix: '/api/v1',
  accessToken: '1234myAccessToken',
  agent: undefined, // e.g. HttpsProxyAgent
  globalParams: {}, // parameters you want available for every request.
  // Global params are only sent of the OpenAPI spec specifies the provided params.
}

client.accessToken

A setter/getter that returns the accessToken that the client is configured to use. You can set this after the class is instantiated, and all subsequent calls will use the newly set accessToken.

client.basePath

A getter that returns the formatted base URL of the endpoint the client is configured to use.

OpenAPI Client methods

The client is dynamically generated from the OpenAPI definition file. Each method is is named after the operationId name of each operation. To see a list of available operations, please see the OpenAPI website.

Every OpenAPI operation has the following signature:

response = await client.operationId([params], [opts])

Performs a call to the given endpoint corresponding with the operationId. Returns a promise resolved with the body of the response, or rejected with an error with the details about the request attached. Rejects if the status > 400.

  • params is an object that includes any of the required or optional endpoint parameters.
  • params.body should be an object which gets serialized to JSON automatically. Any object can live here but refer to the OpenAPI specification for allowed fields in a particular request body. It can also be a function returning an object.
  • If the endpoint accepts binary, params.body can be a Node.js readable stream or a function returning one (e.g. () => fs.createReadStream('./foo')). Using a function is recommended.
// example params
const params = {
  any_param_needed,
  paramsCanAlsoBeCamelCase,
  body: {
    an: 'arbitrary js object',
  },
}

Optional opts can include any property you want passed to node-fetch. The headers property is merged with some defaultHeaders.

// example opts
const opts = {
  headers: {
    // Default headers
    'User-agent': 'netlify-js-client',
    accept: 'application/json',
  },
  // any other properties for node-fetch
}

All operations are conveniently consumed with async/await:

const getSomeData = async () => {
  // Calls may fail!
  try {
    return await client.getSiteDeploy({
      siteId: '1234abcd',
      deploy_id: '4567',
    })
  } catch (error) {
    // handle error
  }
}

If the response includes json in the contentType header, fetch will deserialize the JSON body. Otherwise the text of the response is returned.

API Flow Methods

Some methods have been added in addition to the open API operations that make certain actions simpler to perform.

accessToken = await client.getAccessToken(ticket, [opts])

Pass in a ticket and get back an accessToken. Call this with the response from a client.createTicket({ client_id }) call. Automatically sets the accessToken to this.accessToken and returns accessToken for the consumer to save for later.

Optional opts include:

const opts = {
  poll: 1000, // number of ms to wait between polling
  timeout: 3.6e6, // number of ms to wait before timing out
}

See the authenticating docs for more context.

// example:
const open = require('open') // installed with 'npm i open'

const login = async () => {
  const ticket = await client.createTicket({
    clientId: CLIENT_ID,
  })
  // Open browser for authentication
  await open(`https://app.netlify.com/authorize?response_type=ticket&ticket=${ticket.id}`)
  const accessToken = await client.getAccessToken(ticket)
  // API is also set up to use the returned access token as a side effect
  return accessToken // Save this for later so you can quickly set up an authenticated client
}

deploy = await client.deploy(siteId, buildDir, [opts])

Node.js only: Pass in a siteId, a buildDir (the folder you want to deploy) and an options object to deploy the contents of that folder. Sometimes this method needs to write to a tmpDir. By default tmpDir is a folder in the system temporary directory.

The following paths can be passed in the options:

  • configPath (path to a netlify.toml file that includes redirect rules for the deploy, etc.)
  • fnDir (a folder with lambda functions to deploy)

Optional opts include:

const opts = {
  fnDir: null, // path to a folder of functions to deploy
  branch: null, // branch to pass onto the netlify api
  configPath: null, // path to a netlify.toml file to include in the deploy (e.g. redirect support for manual deploys)
  draft: false, // draft deploy or production deploy
  message: undefined, // a short message to associate with the deploy
  deployTimeout: 1.2e6, // 20 mins
  parallelHash: 100, // number of parallel hashing calls
  parallelUpload: 5, // number of files to upload in parallel
  maxRetry: 5, // number of times to try on failed file uploads
  filter: (filepath) => {
    /* return false to filter a file  from the deploy */
  },
  tmpDir: tempy.directory(), // a temporary directory to zip functions into
  statusCb: (statusObj) => {
    // a callback function to receive status events
    // statusObj: {
    //      type: name-of-step
    //      msg: msg to print
    //      phase: [start, progress, stop]
    //  }
    // See https://github.com/netlify/cli/blob/v2.0.0-beta.3/src/commands/deploy.js#L161-L195
    // for an example of how this can be used.
  },
  // passing a deployId will update an existing deploy based on the provided options
  deployId: null,
}

Proxy support

Node.js only: If this client is used behind a corporate proxy, you can pass an HttpsProxyAgent or any other http.Agent that can handle your situation as agent option:

const HttpsProxyAgent = require('https-proxy-agent')

const proxyUri = 'http(s)://[user:[email protected]]proxyhost:port'
const agent = new HttpsProxyAgent(proxyUri)
const client = new NetlifyAPI('1234myAccessToken', { agent })

UMD Builds

A UMD build is provided for your convenience, however browser support is still experimental. Contributions to improve browser support are welcome.

Contributing

See CONTRIBUTING.md for more info on how to make contributions to this project.

License

MIT. See LICENSE for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].