@puppi/adapter-intercom 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,179 @@
1
+ /**
2
+ * `@puppi/adapter-intercom` lists contacts, conversations, and companies
3
+ * from the Intercom REST API.
4
+ */
5
+ var __runInitializers = (this && this.__runInitializers) || function (thisArg, initializers, value) {
6
+ var useValue = arguments.length > 2;
7
+ for (var i = 0; i < initializers.length; i++) {
8
+ value = useValue ? initializers[i].call(thisArg, value) : initializers[i].call(thisArg);
9
+ }
10
+ return useValue ? value : void 0;
11
+ };
12
+ var __esDecorate = (this && this.__esDecorate) || function (ctor, descriptorIn, decorators, contextIn, initializers, extraInitializers) {
13
+ function accept(f) { if (f !== void 0 && typeof f !== "function") throw new TypeError("Function expected"); return f; }
14
+ var kind = contextIn.kind, key = kind === "getter" ? "get" : kind === "setter" ? "set" : "value";
15
+ var target = !descriptorIn && ctor ? contextIn["static"] ? ctor : ctor.prototype : null;
16
+ var descriptor = descriptorIn || (target ? Object.getOwnPropertyDescriptor(target, contextIn.name) : {});
17
+ var _, done = false;
18
+ for (var i = decorators.length - 1; i >= 0; i--) {
19
+ var context = {};
20
+ for (var p in contextIn) context[p] = p === "access" ? {} : contextIn[p];
21
+ for (var p in contextIn.access) context.access[p] = contextIn.access[p];
22
+ context.addInitializer = function (f) { if (done) throw new TypeError("Cannot add initializers after decoration has completed"); extraInitializers.push(accept(f || null)); };
23
+ var result = (0, decorators[i])(kind === "accessor" ? { get: descriptor.get, set: descriptor.set } : descriptor[key], context);
24
+ if (kind === "accessor") {
25
+ if (result === void 0) continue;
26
+ if (result === null || typeof result !== "object") throw new TypeError("Object expected");
27
+ if (_ = accept(result.get)) descriptor.get = _;
28
+ if (_ = accept(result.set)) descriptor.set = _;
29
+ if (_ = accept(result.init)) initializers.unshift(_);
30
+ }
31
+ else if (_ = accept(result)) {
32
+ if (kind === "field") initializers.unshift(_);
33
+ else descriptor[key] = _;
34
+ }
35
+ }
36
+ if (target) Object.defineProperty(target, contextIn.name, descriptor);
37
+ done = true;
38
+ };
39
+ import { eachPage, HttpStatusError, NotFound, read, TerminalError, OpenApiClient, openApiClientOptions, } from "@puppi/sdk";
40
+ import connector from "./define.js";
41
+ import { operations } from "./operations.js";
42
+ export default connector;
43
+ const auth = connector.auth;
44
+ const COMPANIES_SCOPE = "read_users_companies";
45
+ let Client = (() => {
46
+ let _classSuper = OpenApiClient;
47
+ let _instanceExtraInitializers = [];
48
+ let _contacts_decorators;
49
+ let _conversations_decorators;
50
+ let _companies_decorators;
51
+ return class Client extends _classSuper {
52
+ static {
53
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
54
+ _contacts_decorators = [read];
55
+ _conversations_decorators = [read];
56
+ _companies_decorators = [read];
57
+ __esDecorate(this, null, _contacts_decorators, { kind: "method", name: "contacts", static: false, private: false, access: { has: obj => "contacts" in obj, get: obj => obj.contacts }, metadata: _metadata }, null, _instanceExtraInitializers);
58
+ __esDecorate(this, null, _conversations_decorators, { kind: "method", name: "conversations", static: false, private: false, access: { has: obj => "conversations" in obj, get: obj => obj.conversations }, metadata: _metadata }, null, _instanceExtraInitializers);
59
+ __esDecorate(this, null, _companies_decorators, { kind: "method", name: "companies", static: false, private: false, access: { has: obj => "companies" in obj, get: obj => obj.companies }, metadata: _metadata }, null, _instanceExtraInitializers);
60
+ if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
61
+ }
62
+ constructor(config, secrets = {}, options = {}) {
63
+ super(config, options, ...openApiClientOptions(connector, config, secrets, operations, {
64
+ headers: (version) => ({ "Intercom-Version": version }),
65
+ }));
66
+ __runInitializers(this, _instanceExtraInitializers);
67
+ }
68
+ /**
69
+ * Proves `access_token` with `GET /contacts?per_page=1`.
70
+ * It does not verify the companies scope.
71
+ */
72
+ async check() {
73
+ try {
74
+ await this.read("ListContacts", { query: { per_page: 1 } });
75
+ return { ok: true };
76
+ }
77
+ catch (error) {
78
+ if (error instanceof HttpStatusError && (error.status === 401 || error.status === 403)) {
79
+ const reason = error.status === 403 || errorCode(error) === "token_unauthorized"
80
+ ? "authorization"
81
+ : "authentication";
82
+ return {
83
+ ok: false,
84
+ reason,
85
+ message: `${this.api} ${reason} failed for ${auth.secret} (${error.status} ${error.statusText})`,
86
+ };
87
+ }
88
+ throw error;
89
+ }
90
+ }
91
+ /**
92
+ * Returns each page of contacts. `cursor` is `pages.next.starting_after`
93
+ * from the previous page, and null when that field is absent.
94
+ */
95
+ contacts(args = {}) {
96
+ return this.pages("ListContacts", {
97
+ rows: "data",
98
+ next: "pages.next.starting_after",
99
+ param: "starting_after",
100
+ }, { cursor: args.cursor, query: { per_page: args.perPage } });
101
+ }
102
+ /**
103
+ * Returns each page of conversations. `cursor` is `pages.next.starting_after`
104
+ * from the previous page, and null when that field is absent.
105
+ */
106
+ conversations(args = {}) {
107
+ return this.pages("listConversations", {
108
+ rows: "conversations",
109
+ next: "pages.next.starting_after",
110
+ param: "starting_after",
111
+ }, { cursor: args.cursor, query: { per_page: args.perPage } });
112
+ }
113
+ /**
114
+ * Returns each page of companies. `cursor` is `scroll_param` from the
115
+ * previous page, and null when `data` is empty.
116
+ */
117
+ companies(args = {}) {
118
+ // pages() follows `scroll_param` whenever it is present, including on an empty last page.
119
+ return eachPage({
120
+ cursor: args.cursor ?? null,
121
+ fetchPage: async (cursor) => {
122
+ try {
123
+ const body = await this.read("scrollOverAllCompanies", {
124
+ query: cursor === null ? {} : { scroll_param: cursor },
125
+ });
126
+ return scrollPage(body);
127
+ }
128
+ catch (error) {
129
+ if (error instanceof HttpStatusError &&
130
+ error.status === 401 &&
131
+ errorCode(error) === "token_unauthorized") {
132
+ throw new CompaniesScopeError(error);
133
+ }
134
+ // NotFound drops the body, so a resume 404 is the expired scroll.
135
+ if (cursor !== null && error instanceof NotFound)
136
+ throw new ScrollExpiredError(error);
137
+ throw error;
138
+ }
139
+ },
140
+ });
141
+ }
142
+ };
143
+ })();
144
+ export { Client };
145
+ export class CompaniesScopeError extends TerminalError {
146
+ code = "token_unauthorized";
147
+ constructor(cause) {
148
+ super(`intercom authorization failed for ${auth.secret} (token_unauthorized: ${COMPANIES_SCOPE})`, { cause });
149
+ }
150
+ }
151
+ export class MissingScrollParamError extends TerminalError {
152
+ code = "missing_scroll_param";
153
+ constructor() {
154
+ super("intercom companies scroll returned companies and no scroll_param");
155
+ }
156
+ }
157
+ export class ScrollExpiredError extends TerminalError {
158
+ code = "scroll_expired";
159
+ constructor(cause) {
160
+ super("intercom companies scroll expired; start the companies sync again without this cursor", {
161
+ cause,
162
+ });
163
+ }
164
+ }
165
+ function scrollPage(body) {
166
+ const data = body?.data ?? [];
167
+ if (data.length === 0)
168
+ return { data, next: null };
169
+ const next = body?.scroll_param;
170
+ if (typeof next !== "string" || next === "")
171
+ throw new MissingScrollParamError();
172
+ return { data, next };
173
+ }
174
+ /** `HttpStatusError` keeps 200 characters of the response body. A `code` past that slice is missed. */
175
+ function errorCode(error) {
176
+ const match = /"code"\s*:\s*"([^"]+)"/.exec(error.message);
177
+ return match?.[1];
178
+ }
179
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,OAAO,EACL,QAAQ,EACR,eAAe,EACf,QAAQ,EACR,IAAI,EACJ,aAAa,EAGb,aAAa,EACb,oBAAoB,GACrB,MAAM,YAAY,CAAC;AAEpB,OAAO,SAAS,MAAM,aAAa,CAAC;AAOpC,OAAO,EAAY,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAIvD,eAAe,SAAS,CAAC;AAEzB,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC;AAE5B,MAAM,eAAe,GAAG,sBAAs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