UNPKG

6.83 kBJavaScriptView Raw
1/*
2Copyright 2015, 2016 OpenMarket Ltd
3Copyright 2017 Vector Creations Ltd
4Copyright 2019 The Matrix.org Foundation C.I.C.
5
6Licensed under the Apache License, Version 2.0 (the "License");
7you may not use this file except in compliance with the License.
8You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12Unless required by applicable law or agreed to in writing, software
13distributed under the License is distributed on an "AS IS" BASIS,
14WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15See the License for the specific language governing permissions and
16limitations under the License.
17*/
18
19import {MemoryCryptoStore} from "./crypto/store/memory-crypto-store";
20import {MemoryStore} from "./store/memory";
21import {MatrixScheduler} from "./scheduler";
22import {MatrixClient} from "./client";
23
24export * from "./client";
25export * from "./http-api";
26export * from "./autodiscovery";
27export * from "./sync-accumulator";
28export * from "./errors";
29export * from "./models/event";
30export * from "./models/room";
31export * from "./models/group";
32export * from "./models/event-timeline";
33export * from "./models/event-timeline-set";
34export * from "./models/room-member";
35export * from "./models/room-state";
36export * from "./models/user";
37export * from "./scheduler";
38export * from "./filter";
39export * from "./timeline-window";
40export * from "./interactive-auth";
41export * from "./service-types";
42export * from "./store/memory";
43export * from "./store/indexeddb";
44export * from "./store/session/webstorage";
45export * from "./crypto/store/memory-crypto-store";
46export * from "./crypto/store/indexeddb-crypto-store";
47export * from "./content-repo";
48export const ContentHelpers = import("./content-helpers");
49export {
50 createNewMatrixCall,
51 setAudioOutput as setMatrixCallAudioOutput,
52 setAudioInput as setMatrixCallAudioInput,
53 setVideoInput as setMatrixCallVideoInput,
54} from "./webrtc/call";
55
56
57// expose the underlying request object so different environments can use
58// different request libs (e.g. request or browser-request)
59let requestInstance;
60
61/**
62 * The function used to perform HTTP requests. Only use this if you want to
63 * use a different HTTP library, e.g. Angular's <code>$http</code>. This should
64 * be set prior to calling {@link createClient}.
65 * @param {requestFunction} r The request function to use.
66 */
67export function request(r) {
68 requestInstance = r;
69}
70
71/**
72 * Return the currently-set request function.
73 * @return {requestFunction} The current request function.
74 */
75export function getRequest() {
76 return requestInstance;
77}
78
79/**
80 * Apply wrapping code around the request function. The wrapper function is
81 * installed as the new request handler, and when invoked it is passed the
82 * previous value, along with the options and callback arguments.
83 * @param {requestWrapperFunction} wrapper The wrapping function.
84 */
85export function wrapRequest(wrapper) {
86 const origRequest = requestInstance;
87 requestInstance = function(options, callback) {
88 return wrapper(origRequest, options, callback);
89 };
90}
91
92
93let cryptoStoreFactory = () => new MemoryCryptoStore;
94
95/**
96 * Configure a different factory to be used for creating crypto stores
97 *
98 * @param {Function} fac a function which will return a new
99 * {@link module:crypto.store.base~CryptoStore}.
100 */
101export function setCryptoStoreFactory(fac) {
102 cryptoStoreFactory = fac;
103}
104
105/**
106 * Construct a Matrix Client. Similar to {@link module:client.MatrixClient}
107 * except that the 'request', 'store' and 'scheduler' dependencies are satisfied.
108 * @param {(Object|string)} opts The configuration options for this client. If
109 * this is a string, it is assumed to be the base URL. These configuration
110 * options will be passed directly to {@link module:client.MatrixClient}.
111 * @param {Object} opts.store If not set, defaults to
112 * {@link module:store/memory.MemoryStore}.
113 * @param {Object} opts.scheduler If not set, defaults to
114 * {@link module:scheduler~MatrixScheduler}.
115 * @param {requestFunction} opts.request If not set, defaults to the function
116 * supplied to {@link request} which defaults to the request module from NPM.
117 *
118 * @param {module:crypto.store.base~CryptoStore=} opts.cryptoStore
119 * crypto store implementation. Calls the factory supplied to
120 * {@link setCryptoStoreFactory} if unspecified; or if no factory has been
121 * specified, uses a default implementation (indexeddb in the browser,
122 * in-memory otherwise).
123 *
124 * @return {MatrixClient} A new matrix client.
125 * @see {@link module:client.MatrixClient} for the full list of options for
126 * <code>opts</code>.
127 */
128export function createClient(opts) {
129 if (typeof opts === "string") {
130 opts = {
131 "baseUrl": opts,
132 };
133 }
134 opts.request = opts.request || requestInstance;
135 opts.store = opts.store || new MemoryStore({
136 localStorage: global.localStorage,
137 });
138 opts.scheduler = opts.scheduler || new MatrixScheduler();
139 opts.cryptoStore = opts.cryptoStore || cryptoStoreFactory();
140 return new MatrixClient(opts);
141}
142
143/**
144 * The request function interface for performing HTTP requests. This matches the
145 * API for the {@link https://github.com/request/request#requestoptions-callback|
146 * request NPM module}. The SDK will attempt to call this function in order to
147 * perform an HTTP request.
148 * @callback requestFunction
149 * @param {Object} opts The options for this HTTP request.
150 * @param {string} opts.uri The complete URI.
151 * @param {string} opts.method The HTTP method.
152 * @param {Object} opts.qs The query parameters to append to the URI.
153 * @param {Object} opts.body The JSON-serializable object.
154 * @param {boolean} opts.json True if this is a JSON request.
155 * @param {Object} opts._matrix_opts The underlying options set for
156 * {@link MatrixHttpApi}.
157 * @param {requestCallback} callback The request callback.
158 */
159
160/**
161 * A wrapper for the request function interface.
162 * @callback requestWrapperFunction
163 * @param {requestFunction} origRequest The underlying request function being
164 * wrapped
165 * @param {Object} opts The options for this HTTP request, given in the same
166 * form as {@link requestFunction}.
167 * @param {requestCallback} callback The request callback.
168 */
169
170 /**
171 * The request callback interface for performing HTTP requests. This matches the
172 * API for the {@link https://github.com/request/request#requestoptions-callback|
173 * request NPM module}. The SDK will implement a callback which meets this
174 * interface in order to handle the HTTP response.
175 * @callback requestCallback
176 * @param {Error} err The error if one occurred, else falsey.
177 * @param {Object} response The HTTP response which consists of
178 * <code>{statusCode: {Number}, headers: {Object}}</code>
179 * @param {Object} body The parsed HTTP response body.
180 */