snaptrade-typescript-sdk 1.0.0

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/base.ts ADDED
@@ -0,0 +1,71 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * SnapTrade
5
+ * Connect brokerage accounts to your app for live positions and trading
6
+ *
7
+ * The version of the OpenAPI document: 1.0.0
8
+ * Contact: api@snaptrade.com
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ import { Configuration } from "./configuration";
17
+ // Some imports not used depending on template conditions
18
+ // @ts-ignore
19
+ import globalAxios, { AxiosPromise, AxiosInstance, AxiosRequestConfig } from 'axios';
20
+
21
+ export const BASE_PATH = "https://api.snaptrade.com/api/v1".replace(/\/+$/, "");
22
+
23
+ /**
24
+ *
25
+ * @export
26
+ */
27
+ export const COLLECTION_FORMATS = {
28
+ csv: ",",
29
+ ssv: " ",
30
+ tsv: "\t",
31
+ pipes: "|",
32
+ };
33
+
34
+ /**
35
+ *
36
+ * @export
37
+ * @interface RequestArgs
38
+ */
39
+ export interface RequestArgs {
40
+ url: string;
41
+ options: AxiosRequestConfig;
42
+ }
43
+
44
+ /**
45
+ *
46
+ * @export
47
+ * @class BaseAPI
48
+ */
49
+ export class BaseAPI {
50
+ protected configuration: Configuration | undefined;
51
+
52
+ constructor(configuration?: Configuration, protected basePath: string = BASE_PATH, protected axios: AxiosInstance = globalAxios) {
53
+ if (configuration) {
54
+ this.configuration = configuration;
55
+ this.basePath = configuration.basePath || this.basePath;
56
+ }
57
+ }
58
+ };
59
+
60
+ /**
61
+ *
62
+ * @export
63
+ * @class RequiredError
64
+ * @extends {Error}
65
+ */
66
+ export class RequiredError extends Error {
67
+ name: "RequiredError" = "RequiredError";
68
+ constructor(public field: string, msg?: string) {
69
+ super(msg);
70
+ }
71
+ }
package/common.ts ADDED
@@ -0,0 +1,181 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * SnapTrade
5
+ * Connect brokerage accounts to your app for live positions and trading
6
+ *
7
+ * The version of the OpenAPI document: 1.0.0
8
+ * Contact: api@snaptrade.com
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ import * as crypto from "crypto"
17
+ import { Configuration } from "./configuration";
18
+ import { RequiredError, RequestArgs } from "./base";
19
+ import { AxiosInstance, AxiosResponse } from 'axios';
20
+
21
+ /**
22
+ *
23
+ * @export
24
+ */
25
+ export const DUMMY_BASE_URL = 'https://example.com'
26
+
27
+ /**
28
+ *
29
+ * @throws {RequiredError}
30
+ * @export
31
+ */
32
+ export const assertParamExists = function (functionName: string, paramName: string, paramValue: unknown) {
33
+ if (paramValue === null || paramValue === undefined) {
34
+ throw new RequiredError(paramName, `Required parameter ${paramName} was null or undefined when calling ${functionName}.`);
35
+ }
36
+ }
37
+
38
+ /**
39
+ *
40
+ * @export
41
+ */
42
+ export const setApiKeyToObject = async function (object: any, keyParamName: string, configuration?: Configuration) {
43
+ if (configuration && configuration.apiKey) {
44
+ const localVarApiKeyValue = typeof configuration.apiKey === 'function'
45
+ ? await configuration.apiKey(keyParamName)
46
+ : await configuration.apiKey;
47
+ object[keyParamName] = localVarApiKeyValue;
48
+ }
49
+ }
50
+
51
+ /**
52
+ *
53
+ * @export
54
+ */
55
+ export const setBasicAuthToObject = function (object: any, configuration?: Configuration) {
56
+ if (configuration && (configuration.username || configuration.password)) {
57
+ object["auth"] = { username: configuration.username, password: configuration.password };
58
+ }
59
+ }
60
+
61
+ /**
62
+ *
63
+ * @export
64
+ */
65
+ export const setBearerAuthToObject = async function (object: any, configuration?: Configuration) {
66
+ if (configuration && configuration.accessToken) {
67
+ const accessToken = typeof configuration.accessToken === 'function'
68
+ ? await configuration.accessToken()
69
+ : await configuration.accessToken;
70
+ object["Authorization"] = "Bearer " + accessToken;
71
+ }
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @export
77
+ */
78
+ export const setOAuthToObject = async function (object: any, name: string, scopes: string[], configuration?: Configuration) {
79
+ if (configuration && configuration.accessToken) {
80
+ const localVarAccessTokenValue = typeof configuration.accessToken === 'function'
81
+ ? await configuration.accessToken(name, scopes)
82
+ : await configuration.accessToken;
83
+ object["Authorization"] = "Bearer " + localVarAccessTokenValue;
84
+ }
85
+ }
86
+
87
+ function setFlattenedQueryParams(urlSearchParams: URLSearchParams, parameter: any, key: string = ""): void {
88
+ if (typeof parameter === "object") {
89
+ if (Array.isArray(parameter)) {
90
+ (parameter as any[]).forEach(item => setFlattenedQueryParams(urlSearchParams, item, key));
91
+ }
92
+ else {
93
+ Object.keys(parameter).forEach(currentKey =>
94
+ setFlattenedQueryParams(urlSearchParams, parameter[currentKey], `${key}${key !== '' ? '.' : ''}${currentKey}`)
95
+ );
96
+ }
97
+ }
98
+ else {
99
+ if (urlSearchParams.has(key)) {
100
+ urlSearchParams.append(key, parameter);
101
+ }
102
+ else {
103
+ urlSearchParams.set(key, parameter);
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ *
110
+ * @export
111
+ */
112
+ export const setSearchParams = function (url: URL, ...objects: any[]) {
113
+ const searchParams = new URLSearchParams(url.search);
114
+ setFlattenedQueryParams(searchParams, objects);
115
+ url.search = searchParams.toString();
116
+ }
117
+
118
+ /**
119
+ *
120
+ * @export
121
+ */
122
+ export const serializeDataIfNeeded = function (value: any, requestOptions: any, configuration?: Configuration) {
123
+ const nonString = typeof value !== 'string';
124
+ const needsSerialization = nonString && configuration && configuration.isJsonMime
125
+ ? configuration.isJsonMime(requestOptions.headers['Content-Type'])
126
+ : nonString;
127
+ return needsSerialization
128
+ ? JSON.stringify(value !== undefined ? value : {})
129
+ : (value || "");
130
+ }
131
+
132
+ /**
133
+ *
134
+ * @export
135
+ */
136
+ export const toPathString = function (url: URL) {
137
+ return url.pathname + url.search + url.hash
138
+ }
139
+
140
+ const JSONstringifyOrder = (obj: any) => {
141
+ var allKeys: any = [];
142
+ var seen: any = {};
143
+ JSON.stringify(obj, function (key, value) {
144
+ if (!(key in seen)) {
145
+ allKeys.push(key);
146
+ seen[key] = null;
147
+ }
148
+ return value;
149
+ });
150
+ allKeys.sort();
151
+ return JSON.stringify(obj, allKeys);
152
+ };
153
+
154
+ /**
155
+ *
156
+ * @export
157
+ */
158
+ export const createRequestFunction = function (axiosArgs: RequestArgs, globalAxios: AxiosInstance, BASE_PATH: string, configuration?: Configuration) {
159
+ return <T = unknown, R = AxiosResponse<T>>(axios: AxiosInstance = globalAxios, basePath: string = BASE_PATH) => {
160
+ const axiosRequestArgs = {...axiosArgs.options, url: (configuration?.basePath || basePath) + axiosArgs.url};
161
+
162
+ if (configuration?.consumerKey === undefined) throw Error("Consumer key is required")
163
+ const consumerKey = encodeURI(configuration.consumerKey);
164
+ const requestData = axiosArgs.options.data === undefined || axiosArgs.options.data === '{}' ? null : JSON.parse(axiosArgs.options.data);
165
+ const path = axiosArgs.url.indexOf("?") === -1 ? `${axiosArgs.url}` : `${axiosArgs.url.split("?")[0]}`
166
+ const requestPath = `/api/v1${path}`
167
+ const requestQuery = axiosRequestArgs.url.replace(BASE_PATH, "").replace(path, "").replace("?", "");
168
+ const sigObject = {
169
+ content: requestData,
170
+ path: requestPath,
171
+ query: requestQuery,
172
+ };
173
+ const sigContent = JSONstringifyOrder(sigObject);
174
+ const hmac = crypto.createHmac("sha256", consumerKey);
175
+ const signature = hmac.update(sigContent).digest("base64");
176
+
177
+ axiosRequestArgs.headers["Signature"] = signature;
178
+
179
+ return axios.request<T, R>(axiosRequestArgs);
180
+ };
181
+ }
@@ -0,0 +1,107 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+ /**
4
+ * SnapTrade
5
+ * Connect brokerage accounts to your app for live positions and trading
6
+ *
7
+ * The version of the OpenAPI document: 1.0.0
8
+ * Contact: api@snaptrade.com
9
+ *
10
+ * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
11
+ * https://openapi-generator.tech
12
+ * Do not edit the class manually.
13
+ */
14
+
15
+
16
+ export interface ConfigurationParameters {
17
+ clientId: string
18
+ consumerKey: string
19
+ username?: string;
20
+ password?: string;
21
+ accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
22
+ basePath?: string;
23
+ baseOptions?: any;
24
+ formDataCtor?: new () => any;
25
+ }
26
+
27
+ export class Configuration {
28
+ consumerKey: string
29
+ /**
30
+ * parameter for apiKey security
31
+ * @param name security name
32
+ * @memberof Configuration
33
+ */
34
+ apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
35
+ /**
36
+ * parameter for basic security
37
+ *
38
+ * @type {string}
39
+ * @memberof Configuration
40
+ */
41
+ username?: string;
42
+ /**
43
+ * parameter for basic security
44
+ *
45
+ * @type {string}
46
+ * @memberof Configuration
47
+ */
48
+ password?: string;
49
+ /**
50
+ * parameter for oauth2 security
51
+ * @param name security name
52
+ * @param scopes oauth2 scope
53
+ * @memberof Configuration
54
+ */
55
+ accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
56
+ /**
57
+ * override base path
58
+ *
59
+ * @type {string}
60
+ * @memberof Configuration
61
+ */
62
+ basePath?: string;
63
+ /**
64
+ * base options for axios calls
65
+ *
66
+ * @type {any}
67
+ * @memberof Configuration
68
+ */
69
+ baseOptions?: any;
70
+ /**
71
+ * The FormData constructor that will be used to create multipart form data
72
+ * requests. You can inject this here so that execution environments that
73
+ * do not support the FormData class can still run the generated client.
74
+ *
75
+ * @type {new () => FormData}
76
+ */
77
+ formDataCtor?: new () => any;
78
+
79
+ constructor(param: ConfigurationParameters) {
80
+ this.consumerKey = param.consumerKey;
81
+ this.apiKey = (name: string): string => {
82
+ if (name === "clientId") return param.clientId
83
+ if (name === "timestamp") return Math.round(new Date().getTime() / 1000).toString()
84
+ }
85
+ this.username = param.username;
86
+ this.password = param.password;
87
+ this.accessToken = param.accessToken;
88
+ this.basePath = param.basePath;
89
+ this.baseOptions = param.baseOptions;
90
+ this.formDataCtor = param.formDataCtor;
91
+ }
92
+
93
+ /**
94
+ * Check if the given MIME is a JSON MIME.
95
+ * JSON MIME examples:
96
+ * application/json
97
+ * application/json; charset=UTF8
98
+ * APPLICATION/JSON
99
+ * application/vnd.company+json
100
+ * @param mime - MIME (Multipurpose Internet Mail Extensions)
101
+ * @return True if the given MIME is JSON, false otherwise.
102
+ */
103
+ public isJsonMime(mime: string): boolean {
104
+ const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
105
+ return mime !== null && (jsonMime.test(mime) || mime.toLowerCase() === 'application/json-patch+json');
106
+ }
107
+ }