Coder Social home page Coder Social logo

constant-contact-v3's Introduction

constant-contact-v3

Constant Contact API v3 client for server JavaScript. Requires Node >= 18, or any runtime that supports fetch and either Buffer or btoa. Fully typed with TypeScript, based on Constant Contact's OpenAPI spec. Initially generated using swagger-typescript-api, then modified to add authentication support.

This library will automatically handle refreshing tokens as long as the token remains in memory. You can also provide a callback function to persist the token.

Installation

npm install constant-contact-v3

yarn install constant-contact-v3

pnpm install constant-contact-v3

Options

clientId - Your Constant Contact API key. clientSecret - Your Constant Contact API secret. token - Optional. An existing token to use for authentication. Token is in the format ConstantContactToken. onTokenUpdate - Optional. A callback function that will be called when the token is updated. This is useful for persisting the token. enableThrows - Optional. If true, the client will throw an error when the API returns an error. If false, the client will return the error as part of the response object. The default is false.

export type ConstantContactOptions = {
  clientId: string;
  clientSecret: string;
  token?: ConstantContactToken;
  onTokenUpdate?: (token: ConstantContactToken) => void;
  enableThrows?: boolean;
};

Usage

Before using any of the main routes, you need to acquire an access token. constant-contact-v3 provides all the functions needed for the Authorization Code Flow. Other flows are not currently supported, but would be trivial to add if you are so inclined.

import { ConstantContact } from 'constant-contact-v3';

const api = new ConstantContact({
  clientId: 'YOUR_CLIENT_ID',
  clientSecret: 'YOUR_CLIENT_SECRET',
  onTokenUpdate: token => saveToken(token),
});

const uri = api.oAuth.getAuthorizationURL({
  redirectUri: 'http://localhost:3000/callback',
  state: 'some-state',
  responseType: 'code',
  scope: [
    'account_read',
    'account_update',
    'campaign_data',
    'contact_data',
    'offline_access',
  ],
});

// then redirect the user to the uri. upon return, you will have a code that you can use to get a token.
const code = 'YOUR_CODE';

const tokenRes = await api.oAuth.exchangeAuthorizationCode(
  code,
  environment.constantContact.redirectUri,
);
if (!tokenRes.ok) throw new Error('Failed to exchange code for token');

// then you are ready to go!

constant-contact-v3 will automatically refresh the token as needed. It is saved in the class instance of ConstantContact, so you must create multiple to handle multiple users.

Future Work

  • Add support for other OAuth flows.

constant-contact-v3's People

Contributors

joeyeamigh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.