Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

474 Make Api Service alert on double // #476

Merged
merged 1 commit into from
Aug 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions packages/api-service/src/api-service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BearerTokenProvider } from './bearer-token-provider';
import { isDevelopment } from '@leavittsoftware/titanium-helpers/lib/titanium-dev-detection';
import { BearerTokenProvider } from './bearer-token-provider';
import { HttpError } from './HttpError';
import { ODataDto } from './odata-dto';
import { ODataResponse } from './odata-response';
Expand Down Expand Up @@ -30,6 +31,14 @@ export default class ApiService {
delete this.headers[key];
}

#getFullUri(urlPath: string) {
if (this.baseUrl?.endsWith('/') && urlPath?.startsWith('/') && isDevelopment) {
alert(`API Service Warning: Malformed url, double slashes present. \r\n\r\n${this.baseUrl}${urlPath}`);
}

return `${this.baseUrl}${urlPath}`;
}

async uploadFile<T>(urlPath: string, file: File, onprogress: onProgressCallback, options: ApiServiceRequestOptions | null = null): Promise<ODataResponse<T>> {
return new Promise(async (resolve, reject) => {
if (options?.abortController?.signal && options?.abortController.signal.aborted) {
Expand Down Expand Up @@ -61,7 +70,7 @@ export default class ApiService {
xhr.upload.addEventListener('progress', e => {
onprogress(e, xhr);
});
xhr.open('POST', `${this.baseUrl}${urlPath}`, true);
xhr.open('POST', this.#getFullUri(urlPath), true);

const headers = { ...this.headers };
const token = await this.#tokenProvider._getBearerTokenAsync();
Expand Down Expand Up @@ -119,7 +128,7 @@ export default class ApiService {

let response: Response;
try {
response = await fetch(`${this.baseUrl}${urlPath}`, {
response = await fetch(this.#getFullUri(urlPath), {
method: 'POST',
body: options?.sendAsFormData ? this.#objectToFormData(body) : JSON.stringify(body),
headers: headers,
Expand Down Expand Up @@ -149,7 +158,7 @@ export default class ApiService {

let response: Response;
try {
response = await fetch(`${this.baseUrl}${urlPath}`, {
response = await fetch(this.#getFullUri(urlPath), {
method: 'PATCH',
body: JSON.stringify(body),
headers: headers,
Expand Down Expand Up @@ -178,7 +187,7 @@ export default class ApiService {

let response: Response;
try {
response = await fetch(`${this.baseUrl}${urlPath}`, {
response = await fetch(this.#getFullUri(urlPath), {
method: 'PATCH',
body: JSON.stringify(body),
headers: { ...headers, Prefer: 'return=representation' },
Expand All @@ -199,7 +208,7 @@ export default class ApiService {

let response: Response;
try {
response = await fetch(`${this.baseUrl}${urlPath}`, { method: 'DELETE', headers: headers, signal: options?.abortController?.signal });
response = await fetch(this.#getFullUri(urlPath), { method: 'DELETE', headers: headers, signal: options?.abortController?.signal });
} catch (error) {
return Promise.reject(this.#rewriteFetchErrors(error, 'DELETE', urlPath));
}
Expand All @@ -216,7 +225,7 @@ export default class ApiService {

let response: Response;
try {
response = await fetch(`${this.baseUrl}${urlPath}`, {
response = await fetch(this.#getFullUri(urlPath), {
method: 'GET',
headers: headers,
signal: options?.abortController?.signal,
Expand Down
Loading