UNPKG

11.8 kBTypeScriptView Raw
1import { EventEmitter } from 'events';
2import { BatchDescribeSObjectOptions, DescribeSObjectOptions, DescribeSObjectResult, DescribeGlobalResult } from './describe-result';
3import { Query, QueryResult, ExecuteOptions } from './query';
4import { Record } from './record';
5import { RecordResult } from './record-result';
6import { SObject } from './salesforce-object';
7import { Analytics } from './api/analytics';
8import { Chatter } from './api/chatter';
9import { Metadata } from './api/metadata';
10import { Apex } from './api/apex';
11import { Bulk } from './bulk';
12import { Cache } from './cache'
13import { OAuth2, Streaming } from '.';
14import { HttpApiOptions } from './http-api'
15import { LimitsInfo } from './limits-info';
16
17export type Callback<T> = (err: Error | null, result: T) => void;
18// The type for these options was determined by looking at the usage
19// of the options object in Connection.create and other methods
20// go to http://jsforce.github.io/jsforce/doc/connection.js.html#line568
21// and search for options
22export interface RestApiOptions {
23 headers?: { [x: string]: string }
24 allowRecursive?: boolean;
25 allOrNone?: boolean;
26}
27
28// These are pulled out because according to http://jsforce.github.io/jsforce/doc/connection.js.html#line49
29// the oauth options can either be in the `oauth2` property OR spread across the main connection
30export interface PartialOAuth2Options {
31 clientId?: string;
32 clientSecret?: string;
33 loginUrl?: string;
34 redirectUri?: string;
35 tokenServiceUrl?: string;
36 authzServiceUrl?: string;
37}
38
39export interface RequestInfo {
40 body?: string;
41 headers?: object;
42 method?: string;
43 url?: string;
44}
45
46export interface ConnectionOptions extends PartialOAuth2Options {
47 accessToken?: string;
48 callOptions?: Object;
49 instanceUrl?: string;
50 loginUrl?: string;
51 logLevel?: string;
52 maxRequest?: number;
53 oauth2?: Partial<PartialOAuth2Options>;
54 proxyUrl?: string;
55 httpProxy?: string;
56 redirectUri?: string;
57 refreshToken?: string;
58 refreshFn?: (conn: Connection, callback: Callback<UserInfo>) => Promise<UserInfo>;
59 serverUrl?: string;
60 sessionId?: string;
61 signedRequest?: string | Object;
62 version?: string;
63}
64
65export interface UserInfo {
66 id: string;
67 organizationId: string;
68 url: string;
69}
70
71// The identity URL is a RESTful API to query for additional information
72// about users, such as their username, email address, and org ID.
73// It also returns endpoints that the client can talk to,
74// such as photos for profiles and accessible API endpoints.
75// https://help.salesforce.com/articleView?id=remoteaccess_using_openid.htm
76// https://jsforce.github.io/jsforce/doc/Connection.html#identity
77export interface IdentityInfo {
78 id: string;
79 asserted_user: boolean;
80 user_id: string;
81 organization_id: string;
82 username: string;
83 nick_name: string;
84 display_name: string;
85 email: string;
86 email_verified: boolean;
87 first_name: string | null;
88 last_name: string;
89 timezone: string;
90 photos: {
91 picture: string;
92 thumbnail: string;
93 };
94 addr_street: string | null;
95 addr_city: string | null;
96 addr_state: string | null;
97 addr_country: string | null;
98 addr_zip: string | null;
99 mobile_phone: string | null;
100 mobile_phone_verified: boolean;
101 is_lightning_login_user: boolean;
102 status: {
103 created_date: Date | null;
104 body: string | null;
105 };
106 urls: {
107 enterprise: string;
108 metadata: string;
109 partner: string;
110 rest: string;
111 sobjects: string;
112 search: string;
113 query: string;
114 recent: string;
115 tooling_soap: string;
116 tooling_rest: string;
117 profile: string;
118 feeds: string;
119 groups: string;
120 users: string;
121 feed_items: string;
122 feed_elements: string;
123 custom_domain?: string;
124 };
125 active: boolean;
126 user_type: string;
127 language: string;
128 locale: string;
129 utcOffset: number;
130 last_modified_date: Date;
131 is_app_installed: boolean;
132 // And possible other attributes.
133 [key: string]: any;
134}
135
136export abstract class RestApi {
137 get(path: string, options: object, callback: () => object): Promise<object>;
138 post(path: string, body: object, options: object, callback: () => object): Promise<object>;
139 put(path: string, body: object, options: object, callback: () => object): Promise<object>;
140 patch(path: string, body: object, options: object, callback: () => object): Promise<object>;
141 del(path: string, options: object, callback: () => object): Promise<object>;
142}
143
144export interface ExecuteAnonymousResult {
145 compiled: boolean;
146 compileProblem: string;
147 success: boolean;
148 line: number;
149 column: number;
150 exceptionMessage: string;
151 exceptionStackTrace: string;
152}
153
154export type ConnectionEvent = "refresh";
155
156export interface SearchResult<T> {
157 searchRecords: Record<T>[];
158}
159
160/**
161 * the methods exposed here are done so that a client can use 'declaration augmentation' to get intellisense on their own projects.
162 * for example, given a type
163 *
164 * interface Foo {
165 * thing: string;
166 * yes: boolean;
167 * }
168 *
169 * you can write
170 *
171 * declare module "jsforce" {
172 * interface Connection {
173 * sobject(type: 'Foo'): SObject<Foo>
174 * }
175 * }
176 *
177 * to ensure that you have the correct data types for the various collection names.
178 */
179export abstract class BaseConnection extends EventEmitter {
180 _baseUrl(): string;
181 request<T = object>(info: RequestInfo | string, options?: HttpApiOptions, callback?: (err: Error, Object: T) => void): Promise<T>;
182 query<T>(soql: string, options?: ExecuteOptions, callback?: (err: Error, result: QueryResult<T>) => void): Query<QueryResult<T>>;
183 queryMore<T>(locator: string, options?: ExecuteOptions, callback?: (err: Error, result: QueryResult<T>) => void): Promise<QueryResult<T>>;
184 create<T>(type: string, records: Record<T> | Array<Record<T>>, options?: RestApiOptions,
185 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
186 create<T>(records: Record<T> | Array<Record<T>>, options?: RestApiOptions,
187 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
188 insert<T>(type: string, records: Record<T> | Array<Record<T>>, options?: RestApiOptions,
189 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
190 retrieve<T>(type: string, ids: string | string[], options?: RestApiOptions,
191 callback?: (err: Error, result: Record<T> | Array<Record<T>>) => void): Promise<(Record<T> | Array<Record<T>>)>;
192 update<T>(type: string, records: Record<T> | Array<Record<T>>, options?: RestApiOptions,
193 callback?: (err: Error, result: RecordResult | Array<Record<T>>) => void): Promise<(RecordResult | RecordResult[])>;
194 update<T>(records: Record<T> | Array<Record<T>>, options?: RestApiOptions,
195 callback?: (err: Error, result: RecordResult | Array<Record<T>>) => void): Promise<(RecordResult | RecordResult[])>;
196 upsert<T>(type: string, records: Record<T> | Array<Record<T>>, extIdField: string, options?: RestApiOptions,
197 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
198 upsert<T>(records: Record<T> | Array<Record<T>>, extIdField: string, options?: RestApiOptions,
199 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
200 del<T>(type: string, ids: string | string[], options?: RestApiOptions,
201 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
202 delete<T>(type: string, ids: string | string[], options?: RestApiOptions,
203 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
204 destroy<T>(type: string, ids: string | string[], options?: RestApiOptions,
205 callback?: (err: Error, result: RecordResult | RecordResult[]) => void): Promise<(RecordResult | RecordResult[])>;
206 describe$: {
207 /** Returns a value from the cache if it exists, otherwise calls Connection.describe */
208 (type: string|DescribeSObjectOptions, callback?: (err: Error, result: DescribeSObjectResult) => void): DescribeSObjectResult;
209 clear(): void;
210 }
211 describe(type: string|DescribeSObjectOptions, callback?: (err: Error, result: DescribeSObjectResult) => void): Promise<DescribeSObjectResult>;
212 batchDescribe(options: BatchDescribeSObjectOptions, callback?: (err: Error, result: DescribeSObjectResult[]) => void): Promise<DescribeSObjectResult[]>;
213 describeGlobal$: {
214 /** Returns a value from the cache if it exists, otherwise calls Connection.describeGlobal */
215 (callback?: (err: Error, result: DescribeGlobalResult) => void): DescribeGlobalResult;
216 clear(): void;
217 }
218 describeGlobal<T>(callback?: (err: Error, result: DescribeGlobalResult) => void): Promise<DescribeGlobalResult>;
219 // we want any object to be accepted if the user doesn't decide to give an explicit type
220 sobject<T = object>(resource: string): SObject<T>;
221 recent(callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
222 recent(param: number | string, callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
223 recent(type: string, limit: number, callback?: (err: Error, result: RecordResult[]) => void): Promise<(RecordResult[])>;
224 search<T>(sosl: string, callback?: (err: Error, result: SearchResult<T>) => void): Promise<(SearchResult<T>)>;
225}
226
227export class Connection extends BaseConnection {
228 constructor(params: ConnectionOptions)
229
230 tooling: Tooling;
231 analytics: Analytics;
232 apex: Apex;
233 chatter: Chatter;
234 metadata: Metadata;
235 bulk: Bulk;
236 oauth2: OAuth2;
237 streaming: Streaming;
238 cache: Cache;
239
240 // Specific to Connection
241 instanceUrl: string;
242 version: string;
243 accessToken: string;
244 refreshToken?: string;
245 userInfo?: UserInfo;
246 initialize(options?: ConnectionOptions): void;
247 queryAll<T>(soql: string, options?: object, callback?: (err: Error, result: QueryResult<T>) => void): Query<QueryResult<T>>;
248 authorize(code: string, callback?: (err: Error, res: UserInfo) => void): Promise<UserInfo>;
249 login(user: string, password: string, callback?: (err: Error, res: UserInfo) => void): Promise<UserInfo>;
250 loginByOAuth2(user: string, password: string, callback?: (err: Error, res: UserInfo) => void): Promise<UserInfo>;
251 loginBySoap(user: string, password: string, callback?: (err: Error, res: UserInfo) => void): Promise<UserInfo>;
252 logout(revoke: boolean, callback?: (err: Error, res: undefined) => void): Promise<void>;
253 logout(callback?: (err: Error, res: undefined) => void): Promise<void>;
254 logoutByOAuth2(revoke: boolean, callback?: (err: Error, res: undefined) => void): Promise<void>;
255 logoutByOAuth2(callback?: (err: Error, res: undefined) => void): Promise<void>;
256 logoutBySoap(revoke: boolean, callback?: (err: Error, res: undefined) => void): Promise<void>;
257 logoutBySoap(callback?: (err: Error, res: undefined) => void): Promise<void>;
258 limits(callback?: (err: Error, res: undefined) => void): Promise<LimitsInfo>;
259 identity(callback?: (err: Error, res: IdentityInfo) => void): Promise<IdentityInfo>;
260}
261
262export class Tooling extends BaseConnection {
263 _logger: any;
264
265 // Specific to tooling
266 executeAnonymous(body: string, callback?: (err: Error, res: any) => void): Promise<ExecuteAnonymousResult>;
267}
268
\No newline at end of file