Skip to content

Commit 064f277

Browse files
authored
fix: improve document URL handling (#63)
- Avoid trailing slashes - Always construct URLs with Axios API
1 parent 05dd98d commit 064f277

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

src/roc/Roc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,15 @@ export class Roc<PublicUserInfo = unknown, PrivateUserInfo = unknown> {
8888
data: RocEntryDocument<ContentType, IdType>,
8989
options?: RocDocumentOptions,
9090
) {
91-
const url = new URL(`entry/${data._id}/`, this.dbUrl).href;
91+
const url = new URL(`entry/${data._id}`, this.dbUrl).href;
9292
return new RocDocument(data, createAxios(url, this.accessToken), options);
9393
}
9494

9595
public getDocument<ContentType, IdType>(
9696
uuid: string,
9797
options?: RocDocumentOptions,
9898
): RocDocument<ContentType, IdType> {
99-
const url = new URL(`entry/${uuid}/`, this.dbUrl).href;
99+
const url = new URL(`entry/${uuid}`, this.dbUrl).href;
100100
return new RocDocument(uuid, createAxios(url, this.accessToken), options);
101101
}
102102

src/roc/RocDocument.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ export class RocDocument<
5252
responseType: 'text' | 'arraybuffer' | 'blob',
5353
axiosOptions?: RocAxiosRequestOptions,
5454
): Promise<Buffer | string> {
55-
const url = new URL(name, this.getBaseUrl()).href;
56-
const response = await this.request({
57-
url,
55+
const response = await this.request.get(name, {
5856
responseType,
5957
...axiosOptions,
6058
});
@@ -68,7 +66,7 @@ export class RocDocument<
6866
if (rev) {
6967
throw new Error('UNIMPLEMENTED fetch with rev');
7068
}
71-
const response = await this.request.get('/', axiosOptions);
69+
const response = await this.request.get('', axiosOptions);
7270
this.value = response.data;
7371
return response.data;
7472
}
@@ -111,7 +109,7 @@ export class RocDocument<
111109
}
112110

113111
// Send the new doc
114-
await this.request.put('/', newDoc, axiosOptions);
112+
await this.request.put('', newDoc, axiosOptions);
115113

116114
// Get the new document
117115
// With updated properties ($lastModifification...)
@@ -139,14 +137,15 @@ export class RocDocument<
139137
}
140138

141139
public async delete(axiosOptions?: RocAxiosRequestOptions) {
142-
const response = await this.request.delete('/', axiosOptions);
140+
const response = await this.request.delete('', axiosOptions);
143141
if (response.data.ok) {
144142
this.value = undefined;
145143
this.deleted = true;
146144
} else {
147145
throw new Error('document was not deleted');
148146
}
149147
}
148+
150149
public getAttachment(name: string): RocAttachment {
151150
if (this.value === undefined) {
152151
throw new RocClientError(
@@ -161,7 +160,7 @@ export class RocDocument<
161160
return {
162161
...attachments[name],
163162
name,
164-
url: `${this.getBaseUrl()}${name}`,
163+
url: this.request.getUri({ url: name }),
165164
};
166165
}
167166

@@ -182,9 +181,6 @@ export class RocDocument<
182181
return response.data;
183182
}
184183

185-
protected getBaseUrl() {
186-
return this.request.defaults.baseURL || '';
187-
}
188184
private async _fetchIfUnfetched() {
189185
if (this.value === undefined) {
190186
await this.fetch();

0 commit comments

Comments
 (0)