@pipedream/lob
Version:
Pipedream Lob Components
87 lines (86 loc) • 2.97 kB
JavaScript
import { defineApp } from "@pipedream/types";
import constants from "./common/constants.mjs";
import { Configuration, AddressesApi, AddressEditable, PostcardsApi, PostcardEditable, LettersApi, LetterEditable, } from "@lob/lob-typescript-sdk";
export default defineApp({
type: "app",
app: "lob",
propDefinitions: {
addressId: {
type: "string",
label: "Address",
async options() {
const addresses = await this.listAllAddresses();
return addresses.map(({ id, name, }) => ({
label: name,
value: id,
}));
},
},
postcardId: {
type: "string",
label: "Postcard",
async options() {
const postcards = await this.listAllPostcards();
return postcards.map(({ id, description, }) => ({
label: description,
value: id,
}));
},
},
},
methods: {
_authConfig() {
return new Configuration({
username: this.$auth.secret_api_key,
});
},
_letterApi() {
return new LettersApi(this._authConfig());
},
_postcardsApi() {
return new PostcardsApi(this._authConfig());
},
_addressApi() {
return new AddressesApi(this._authConfig());
},
async listAllAddresses() {
return this.paginate(this._addressApi());
},
async createAddress(opts = {}) {
const addressApi = this._addressApi();
const addressCreate = new AddressEditable(opts);
return addressApi.create(addressCreate);
},
async listAllPostcards() {
return this.paginate(this._postcardsApi());
},
async createLetter(opts = {}) {
const letterApi = this._letterApi();
const letter = new LetterEditable(opts);
return letterApi.create(letter);
},
async createPostcard(opts = {}) {
const postCardApi = this._postcardsApi();
const postcard = new PostcardEditable(opts);
return postCardApi.create(postcard);
},
async retrievePostcard(id) {
const postCardApi = this._postcardsApi();
return postCardApi.get(id);
},
async cancelPostcard(id) {
const postCardApi = this._postcardsApi();
return postCardApi.cancel(id);
},
async paginate(api) {
let nextPageToken;
const list = [];
do {
const response = await api.list(constants.MAX_LIMIT, undefined, nextPageToken);
list.push(...response.data);
nextPageToken = response.nextPageToken;
} while (nextPageToken);
return list;
},
},
});