• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ringcentral / ringcentral-js / 8789319645

22 Apr 2024 06:48PM UTC coverage: 93.418%. Remained the same
8789319645

push

github

SushilMallRC
Add comments for code

574 of 702 branches covered (81.77%)

Branch coverage included in aggregate %.

1924 of 1972 relevant lines covered (97.57%)

42.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/sdk/src/SDK.ts
1
import {EventEmitter} from 'events';
4✔
2
import Cache from './core/Cache';
4✔
3
import Externals, {ExternalsOptions} from './core/Externals';
4✔
4
import * as Constants from './core/Constants';
4✔
5
import Client, {ApiError, CreateRequestOptions} from './http/Client';
4✔
6
import Platform, {
4✔
7
    CreateUrlOptions,
8
    LoginOptions,
9
    LoginUrlOptions,
10
    LoginWindowOptions,
11
    PlatformOptions,
12
    SendOptions,
13
} from './platform/Platform';
14
import {AuthData} from './platform/Auth';
15

16
export {
17
    Cache,
4✔
18
    Externals,
4✔
19
    LoginOptions,
20
    LoginUrlOptions,
21
    LoginWindowOptions,
22
    CreateUrlOptions,
23
    SendOptions,
24
    AuthData,
25
    ExternalsOptions,
26
    CreateRequestOptions,
27
    ApiError,
28
};
29

30
let defaultExternals: ExternalsOptions = {};
4✔
31

32
export const setDefaultExternals = (externals: ExternalsOptions) => (defaultExternals = externals);
4✔
33

34
export class SDK {
4✔
35
    private _externals: Externals;
36

37
    private _cache: Cache;
38

39
    private _client: Client;
40

41
    private _platform: Platform;
42

43
    public static version = Constants.version;
4✔
44

45
    public static EventEmitter = EventEmitter;
4✔
46

47
    /**
48
     * Sandbox Server https://platform.devtest.ringcentral.com
49
     * Production Server https://platform.ringcentral.com
50
     */
51
    public static server = {
4✔
52
        sandbox: 'https://platform.devtest.ringcentral.com',
53
        production: 'https://platform.ringcentral.com',
54
    };
55

56
    /**
57
     * Handles login redirect by sending authentication response to the opener window.
58
     * @param origin The origin to post the message to.
59
     * @param win The window object. If not provided, defaults to the global window object.
60
     */
61
    public static handleLoginRedirect(origin, win) {
4✔
62
        // Use the provided window object or default to the global window object.
63
        win = win || window;
2!
64

65
        // Get the authentication response from the location search or hash.
66
        const response = win.location.search ? win.location.search : win.location.hash;
2✔
67

68
        // Create a message object containing the authentication response.
69
        const msg = {};
2✔
70
        msg[Constants.authResponseProperty] = response;
2✔
71

72
        // Post the message to the opener window with the specified origin.
73
        win.opener.postMessage(msg, origin || win.location.origin);
2✔
74
    }
75

76
    /**
77
     * Constructs a new SDK instance with the provided options.
78
     * @param options The SDK options.
79
     */
80
    public constructor(options: SDKOptions = {}) {
98✔
81
        // Destructure options or use default values.
82
        const {cachePrefix, defaultRequestInit, handleRateLimit} = options;
291✔
83

84
        // Warn if using sandbox server (deprecated).
85
        if (options?.server === SDK.server.sandbox) {
97!
86
            // eslint-disable-next-line no-console
87
            console.warn('Sandbox support is deprecated. Please migrate your application to Production Server.');
×
88
        }
89

90
        // Initialize external dependencies.
91
        this._externals = new Externals({
97✔
92
            ...defaultExternals,
93
            ...options,
94
        });
95

96
        // Initialize cache.
97
        this._cache = new Cache({
97✔
98
            externals: this._externals,
99
            prefix: cachePrefix,
100
        });
101

102
        // Initialize client.
103
        this._client = new Client({
97✔
104
            externals: this._externals,
105
            defaultRequestInit,
106
        });
107

108
        // Initialize platform.
109
        this._platform = new Platform({
97✔
110
            ...options,
111
            externals: this._externals,
112
            client: this._client,
113
            cache: this._cache,
114
            handleRateLimit,
115
        });
116
    }
117

118
    public platform() {
4✔
119
        return this._platform;
153✔
120
    }
121

122
    public client() {
4✔
123
        return this._client;
30✔
124
    }
125

126
    public cache() {
4✔
127
        return this._cache;
149✔
128
    }
129

130
    public externals() {
4✔
131
        return this._externals;
1✔
132
    }
133

134
    /* istanbul ignore next */
135
    public send = async (options: SendOptions): Promise<Response> => this.platform().send(options);
136

137
    /* istanbul ignore next */
138
    public get = async (url, query?, options?: SendOptions): Promise<Response> =>
139
        this.platform().send({method: 'GET', url, query, ...options});
140

141
    /* istanbul ignore next */
142
    public post = async (url, body?, query?, options?: SendOptions): Promise<Response> =>
143
        this.platform().send({method: 'POST', url, query, body, ...options});
144

145
    /* istanbul ignore next */
146
    public put = async (url, body?, query?, options?: SendOptions): Promise<Response> =>
147
        this.platform().send({method: 'PUT', url, query, body, ...options});
148

149
    /* istanbul ignore next */
150
    public patch = async (url, body?, query?, options?: SendOptions): Promise<Response> =>
151
        this.platform().send({method: 'PATCH', url, query, body, ...options});
152

153
    /* istanbul ignore next */
154
    public delete = async (url, query?, options?: SendOptions): Promise<Response> =>
155
        this.platform().send({method: 'DELETE', url, query, ...options});
156

157
    /* istanbul ignore next */
158
    public login = async (options: LoginOptions): Promise<Response> => this.platform().login(options);
159

160
    /* istanbul ignore next */
161
    public ensureLoggedIn = async (): Promise<Response> => this.platform().ensureLoggedIn();
162

163
    /* istanbul ignore next */
164
    public loginUrl = (options: LoginUrlOptions): string => this.platform().loginUrl(options);
165

166
    /* istanbul ignore next */
167
    public createUrl = (path, options: CreateUrlOptions): string => this.platform().createUrl(path, options);
168

169
    /* istanbul ignore next */
170
    public signUrl = async (path): Promise<string> => this.platform().signUrl(path);
171

172
    /* istanbul ignore next */
173
    public parseLoginRedirect = (url): any => this.platform().parseLoginRedirect(url);
174

175
    /* istanbul ignore next */
176
    public logout = async (): Promise<Response> => this.platform().logout();
177

178
    /* istanbul ignore next */
179
    public loginWindow = async (options: LoginWindowOptions): Promise<LoginOptions> =>
180
        this.platform().loginWindow(options);
181

182
    /* istanbul ignore next */
183
    public refresh = async (): Promise<Response> => this.platform().refresh();
184

185
    /* istanbul ignore next */
186
    public multipart = async (response: Response): Promise<Response[]> => this.client().multipart(response);
187

188
    /* istanbul ignore next */
189
    public getContentType = (response: Response): string => this.client().getContentType(response);
190

191
    /* istanbul ignore next */
192
    public isMultipart = (response: Response) => this.client().isMultipart(response);
193

194
    /* istanbul ignore next */
195
    public isJson = (response: Response) => this.client().isJson(response);
196

197
    /* istanbul ignore next */
198
    public error = (response: Response): Promise<string> => this.client().error(response);
199
}
4✔
200
export interface SDKOptions extends PlatformOptions, ExternalsOptions {
201
    cachePrefix?: string;
202
    defaultRequestInit?: CreateRequestOptions;
203
    handleRateLimit?: boolean | number;
204
}
205

206
export default SDK;
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc