@flashbacktech/flashbackclient 0.0.47 → 0.0.48

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.
@@ -1,13 +1,23 @@
1
1
  import { CreateUnitRequest, CreateUnitResponse, CreateRepoRequest, CreateRepoResponse, CreateRepoKeyRequest, CreateRepoKeyResponse, GetUnitsResponse, GetReposResponse, GetRepoKeysResponse, UpdateUnitRequest, UpdateUnitResponse, ActionResponse, UpdateRepoRequest, UpdateRepoResponse, UpdateRepoKeyRequest, UpdateRepoKeyResponse } from './types/storage';
2
2
  import { IApiClient, ProviderType } from './interfaces';
3
+ import { OAuth2ResponseDTO, RefreshTokenResponse } from './types/auth';
4
+ interface ErrorResponse {
5
+ message?: string;
6
+ [key: string]: any;
7
+ }
8
+ export declare class HttpError extends Error {
9
+ status: number;
10
+ statusText: string;
11
+ data: ErrorResponse;
12
+ constructor(status: number, statusText: string, data: ErrorResponse);
13
+ }
3
14
  export declare class ApiClient implements IApiClient {
4
15
  private baseURL;
5
16
  private headers;
6
17
  constructor(baseURL?: string);
7
18
  setAuthToken: (token: string | null) => void;
8
19
  authenticate: (token: string, provider: ProviderType) => Promise<any>;
9
- exchangeCode: (code: string, provider: ProviderType) => Promise<any>;
10
- private exchangeGoogleCode;
20
+ exchangeCode: (code: string, provider: ProviderType) => Promise<OAuth2ResponseDTO>;
11
21
  private exchangeGithubCode;
12
22
  private exchangeWeb3StellarCode;
13
23
  /**
@@ -16,12 +26,13 @@ export declare class ApiClient implements IApiClient {
16
26
  * @param provider - The provider to refresh the token for
17
27
  * @returns The refreshed token
18
28
  */
19
- refreshToken: (refreshToken: string, provider: ProviderType) => Promise<any>;
29
+ refreshToken: (refreshToken: string, provider: ProviderType) => Promise<RefreshTokenResponse>;
20
30
  private authenticateWeb3Stellar;
31
+ private makeRequest;
21
32
  private authenticateGoogle;
22
33
  private authenticateGithub;
23
- private refreshTokenGoogle;
24
- private makeRequest;
34
+ private refreshGoogleToken;
35
+ private exchangeGoogleCode;
25
36
  createStorageUnit: (data: CreateUnitRequest) => Promise<CreateUnitResponse>;
26
37
  getStorageUnits: () => Promise<GetUnitsResponse>;
27
38
  updateStorageUnit: (unitId: string, data: UpdateUnitRequest) => Promise<UpdateUnitResponse>;
@@ -35,3 +46,4 @@ export declare class ApiClient implements IApiClient {
35
46
  updateRepoKey: (repoId: string, data: UpdateRepoKeyRequest) => Promise<UpdateRepoKeyResponse>;
36
47
  deleteRepoKey: (repoId: string, keyId: string) => Promise<ActionResponse>;
37
48
  }
49
+ export {};
@@ -1,7 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiClient = void 0;
3
+ exports.ApiClient = exports.HttpError = void 0;
4
4
  const interfaces_1 = require("./interfaces");
5
+ class HttpError extends Error {
6
+ constructor(status, statusText, data) {
7
+ super(`HTTP Error ${status}: ${statusText}`);
8
+ this.status = status;
9
+ this.statusText = statusText;
10
+ this.data = data;
11
+ this.name = 'HttpError'; // This helps in instanceof checks
12
+ }
13
+ }
14
+ exports.HttpError = HttpError;
5
15
  class ApiClient {
6
16
  constructor(baseURL = 'https://api.flashback.tech') {
7
17
  this.setAuthToken = (token) => {
@@ -42,18 +52,6 @@ class ApiClient {
42
52
  throw new Error(`Unsupported provider: ${provider}`);
43
53
  }
44
54
  };
45
- this.exchangeGoogleCode = async (code) => {
46
- const response = await fetch(`${this.baseURL}/auth/google/exchange`, {
47
- method: 'POST',
48
- headers: this.headers,
49
- body: JSON.stringify({ code }),
50
- });
51
- if (!response.ok) {
52
- throw new Error(`HTTP error! status: ${response.status}`);
53
- }
54
- const ret = await response.json();
55
- return ret;
56
- };
57
55
  this.exchangeGithubCode = async (code) => {
58
56
  throw new Error('Not implemented');
59
57
  };
@@ -69,7 +67,7 @@ class ApiClient {
69
67
  this.refreshToken = async (refreshToken, provider) => {
70
68
  switch (provider) {
71
69
  case interfaces_1.ProviderType.GOOGLE:
72
- return this.refreshTokenGoogle(refreshToken);
70
+ return this.refreshGoogleToken(refreshToken);
73
71
  case interfaces_1.ProviderType.GITHUB:
74
72
  // TODO: Implement refresh token for Github
75
73
  throw new Error('Not implemented');
@@ -80,44 +78,6 @@ class ApiClient {
80
78
  this.authenticateWeb3Stellar = async (token) => {
81
79
  throw new Error('Not implemented');
82
80
  };
83
- this.authenticateGoogle = async (token) => {
84
- this.setAuthToken(token);
85
- const response = await fetch(`${this.baseURL}/auth/google`, {
86
- method: 'POST',
87
- headers: this.headers,
88
- body: JSON.stringify({ token }),
89
- });
90
- if (!response.ok) {
91
- throw new Error(`HTTP error! status: ${response.status}`);
92
- }
93
- const ret = await response.json();
94
- return ret;
95
- };
96
- this.authenticateGithub = async (code) => {
97
- this.setAuthToken(code);
98
- const response = await fetch(`${this.baseURL}/auth/github`, {
99
- method: 'POST',
100
- headers: this.headers,
101
- body: JSON.stringify({ code }),
102
- });
103
- if (!response.ok) {
104
- throw new Error(`HTTP error! status: ${response.status}`);
105
- }
106
- const ret = await response.json();
107
- return ret;
108
- };
109
- this.refreshTokenGoogle = async (refreshToken) => {
110
- const response = await fetch(`${this.baseURL}/auth/google/refresh`, {
111
- method: 'POST',
112
- headers: this.headers,
113
- body: JSON.stringify({ refresh_token: refreshToken }),
114
- });
115
- if (!response.ok) {
116
- throw new Error(`HTTP error! status: ${response.status}`);
117
- }
118
- const ret = await response.json();
119
- return ret;
120
- };
121
81
  this.makeRequest = async (path, method, data) => {
122
82
  const options = {
123
83
  method,
@@ -125,50 +85,65 @@ class ApiClient {
125
85
  body: data ? JSON.stringify(data) : null,
126
86
  };
127
87
  const response = await fetch(`${this.baseURL}/${path}`, options);
88
+ const responseData = await response.json();
128
89
  if (!response.ok) {
129
- throw new Error(`HTTP error! status: ${response.status}`);
90
+ throw new HttpError(response.status, response.statusText, responseData);
130
91
  }
131
- const ret = await response.json();
132
- return ret;
92
+ return responseData;
93
+ };
94
+ ////// Auth API
95
+ this.authenticateGoogle = async (token) => {
96
+ this.setAuthToken(token);
97
+ return this.makeRequest('auth/google', 'POST', { token });
98
+ };
99
+ this.authenticateGithub = async (code) => {
100
+ this.setAuthToken(code);
101
+ return this.makeRequest('auth/github', 'POST', { code });
102
+ };
103
+ this.refreshGoogleToken = async (refreshToken) => {
104
+ return this.makeRequest('auth/google/refresh', 'POST', { refresh_token: refreshToken });
105
+ };
106
+ this.exchangeGoogleCode = async (code) => {
107
+ return this.makeRequest('auth/google/exchange', 'POST', { code });
133
108
  };
134
109
  ////// Units API
135
110
  this.createStorageUnit = async (data) => {
136
111
  return this.makeRequest('unit', 'POST', data);
137
112
  };
138
113
  this.getStorageUnits = async () => {
139
- return this.makeRequest('unit', 'GET', {});
114
+ return this.makeRequest('unit', 'GET', null);
140
115
  };
141
116
  this.updateStorageUnit = async (unitId, data) => {
142
117
  return this.makeRequest(`unit/${unitId}`, 'PUT', data);
143
118
  };
144
119
  this.deleteStorageUnit = async (unitId) => {
145
- return this.makeRequest(`unit/${unitId}`, 'DELETE', {});
120
+ return this.makeRequest(`unit/${unitId}`, 'DELETE', null);
146
121
  };
147
122
  ////// Repos API
148
123
  this.createRepo = async (data) => {
149
124
  return this.makeRequest('repo', 'POST', data);
150
125
  };
151
126
  this.getRepos = async () => {
152
- return this.makeRequest('repo', 'GET', {});
127
+ return this.makeRequest('repo', 'GET', null);
153
128
  };
154
129
  this.updateRepo = async (repoId, data) => {
155
130
  return this.makeRequest(`repo/${repoId}`, 'PUT', data);
156
131
  };
157
132
  this.deleteRepo = async (repoId) => {
158
- return this.makeRequest(`repo/${repoId}`, 'DELETE', {});
133
+ return this.makeRequest(`repo/${repoId}`, 'DELETE', null);
159
134
  };
160
135
  ////// Keys API
161
136
  this.createRepoKey = async (data) => {
162
137
  return this.makeRequest(`repo/${data.repoId}/apikey`, 'POST', data);
163
138
  };
164
139
  this.getRepoKeys = async (repoId) => {
165
- return this.makeRequest(`repo/${repoId}/apikey`, 'GET', {});
140
+ return this.makeRequest(`repo/${repoId}/apikey`, 'GET', null);
166
141
  };
167
142
  this.updateRepoKey = async (repoId, data) => {
168
143
  return this.makeRequest(`repo/${repoId}/apikey`, 'PUT', data);
169
144
  };
170
145
  this.deleteRepoKey = async (repoId, keyId) => {
171
- return this.makeRequest(`repo/${repoId}/apikey/${keyId}`, 'DELETE', {});
146
+ return this.makeRequest(`repo/${repoId}/apikey/${keyId}`, 'DELETE', null);
172
147
  };
173
148
  this.baseURL = baseURL;
174
149
  this.headers = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flashbacktech/flashbackclient",
3
- "version": "0.0.47",
3
+ "version": "0.0.48",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },