Skip to content

Commit c4217f5

Browse files
fix(sdk): apply PR review feedback for v0.2.0
- Add headers to RequestOptions for type-safe header passing - Make accessToken/token optional in AuthResponse - Remove (options as any).headers cast in client.ts - Add public getBaseUrl()/getApiKey() getters to UrBackendClient - Fix refreshToken() to use RequestOptions instead of any - Fix socialStart() to use public getters instead of bracket access - Fix socialExchange() return type to SocialExchangeResponse - Fix getSchema() to extract response.collection - Fix delete() to validate response keys properly - Update schema/database test mocks to match real API shapes - Make beforeEach in auth test async and await logout() Agent-Logs-Url: https://github.com/geturbackend/urBackend/sessions/2c6f46de-4645-4d3e-8ae1-10e938a9e258 Co-authored-by: yash-pouranik <172860064+yash-pouranik@users.noreply.github.com>
1 parent 2b916ab commit c4217f5

8 files changed

Lines changed: 40 additions & 20 deletions

File tree

‎sdks/urbackend-sdk/src/client.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ export class UrBackendClient {
6363
return this._mail;
6464
}
6565

66+
public getBaseUrl(): string {
67+
return this.baseUrl;
68+
}
69+
70+
public getApiKey(): string {
71+
return this.apiKey;
72+
}
73+
6674
/**
6775
* Internal request handler
6876
*/
@@ -83,8 +91,8 @@ export class UrBackendClient {
8391
}
8492

8593
// Merge custom headers from options if provided
86-
if ((options as any).headers) {
87-
Object.assign(headers, (options as any).headers);
94+
if (options.headers) {
95+
Object.assign(headers, options.headers);
8896
}
8997

9098
let requestBody: BodyInit | undefined;

‎sdks/urbackend-sdk/src/modules/auth.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
ResetPasswordPayload,
1313
SocialExchangePayload,
1414
SocialExchangeResponse,
15-
ApiResponse,
15+
RequestOptions,
1616
} from '../types';
1717
import { AuthError } from '../errors';
1818

@@ -140,7 +140,7 @@ export class AuthModule {
140140
* @param refreshToken Optional refresh token for header mode. If omitted, uses cookie mode.
141141
*/
142142
public async refreshToken(refreshToken?: string): Promise<AuthResponse> {
143-
const options: any = {};
143+
const options: RequestOptions = {};
144144
if (refreshToken) {
145145
options.headers = { 'x-refresh-token': refreshToken, 'x-refresh-token-mode': 'header' };
146146
} else {
@@ -157,17 +157,16 @@ export class AuthModule {
157157
* Redirect the user's browser to this URL to begin the flow.
158158
*/
159159
public socialStart(provider: 'github' | 'google'): string {
160-
return `${this.client['baseUrl']}/api/userAuth/social/${provider}/start?key=${this.client['apiKey']}`;
160+
return `${this.client.getBaseUrl()}/api/userAuth/social/${provider}/start?key=${this.client.getApiKey()}`;
161161
}
162162

163163
/**
164164
* Exchange social auth rtCode for a refresh token
165165
*/
166-
public async socialExchange(payload: SocialExchangePayload): Promise<ApiResponse<SocialExchangeResponse>> {
167-
const response = await this.client.request<ApiResponse<SocialExchangeResponse>>('POST', '/api/userAuth/social/exchange', {
166+
public async socialExchange(payload: SocialExchangePayload): Promise<SocialExchangeResponse> {
167+
return this.client.request<SocialExchangeResponse>('POST', '/api/userAuth/social/exchange', {
168168
body: payload,
169169
});
170-
return response;
171170
}
172171

173172
/**

‎sdks/urbackend-sdk/src/modules/database.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,18 @@ export class DatabaseModule {
8282
* Delete a document by its ID
8383
*/
8484
public async delete(collection: string, id: string, token?: string): Promise<{ deleted: boolean }> {
85-
const result = await this.client.request<any>('DELETE', `/api/data/${collection}/${id}`, {
86-
token
87-
});
88-
// The API might return { message: "Document deleted", id: "..." }
89-
return { deleted: !!result };
85+
const result = await this.client.request<{ message?: string; id?: string } | null>(
86+
'DELETE',
87+
`/api/data/${collection}/${id}`,
88+
{ token },
89+
);
90+
91+
const deleted =
92+
typeof result === 'object' &&
93+
result !== null &&
94+
(result.id === id || result.message === 'Document deleted');
95+
96+
return { deleted };
9097
}
9198

9299
/**

‎sdks/urbackend-sdk/src/modules/schema.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export class SchemaModule {
88
* Fetch the schema definition for a collection
99
*/
1010
public async getSchema(collection: string): Promise<CollectionSchema> {
11-
return this.client.request<CollectionSchema>('GET', `/api/schemas/${collection}`);
11+
const response = await this.client.request<{
12+
message: string;
13+
collection: CollectionSchema;
14+
}>('GET', `/api/schemas/${collection}`);
15+
16+
return response.collection;
1217
}
1318
}

‎sdks/urbackend-sdk/src/types/index.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface RequestOptions {
99
token?: string;
1010
isMultipart?: boolean;
1111
credentials?: RequestCredentials;
12+
headers?: Record<string, string>;
1213
}
1314

1415
export interface QueryParams {
@@ -82,9 +83,9 @@ export interface AuthUser {
8283
}
8384

8485
export interface AuthResponse {
85-
accessToken: string;
86+
accessToken?: string;
8687
/** @deprecated use accessToken instead */
87-
token: string;
88+
token?: string;
8889
expiresIn?: string;
8990
userId?: string;
9091
user?: AuthUser;

‎sdks/urbackend-sdk/tests/auth.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import urBackend from '../src/index';
44
const mockApiKey = 'pk_live_test';
55
const client = urBackend({ apiKey: mockApiKey });
66

7-
beforeEach(() => {
7+
beforeEach(async () => {
88
vi.resetAllMocks();
9-
client.auth.logout();
9+
await client.auth.logout();
1010
});
1111

1212
test('signUp returns user object on success', async () => {

‎sdks/urbackend-sdk/tests/database.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ test('delete returns { deleted: true } and handles token', async () => {
115115
const fetchMock = vi.fn().mockResolvedValue({
116116
ok: true,
117117
headers: new Headers({ 'content-type': 'application/json' }),
118-
json: () => Promise.resolve({ success: true, data: { message: 'Deleted' } }),
118+
json: () => Promise.resolve({ success: true, data: { message: 'Document deleted', id: 'id-1' } }),
119119
});
120120
vi.stubGlobal('fetch', fetchMock);
121121

‎sdks/urbackend-sdk/tests/schema.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ test('getSchema returns collection schema', async () => {
2222
vi.fn().mockResolvedValue({
2323
ok: true,
2424
headers: new Headers({ 'content-type': 'application/json' }),
25-
json: () => Promise.resolve({ success: true, data: mockSchema }),
25+
json: () => Promise.resolve({ message: 'Schema exists', collection: mockSchema }),
2626
}),
2727
);
2828

0 commit comments

Comments
 (0)