Skip to content

Commit 77a7de0

Browse files
committed
refactor remainig endpoints
1 parent 86efcb3 commit 77a7de0

File tree

3 files changed

+14
-79
lines changed

3 files changed

+14
-79
lines changed

src/Endpoint/User/PostChangePasswordEndpoint.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NetworkError, ParseError } from '../../Error/index.js';
21
import { FetchHelper, Logger, ServiceResolver } from '../../Service/index.js';
32
import { UniqueUserIdentifier } from '../../Type/Definition/index.js';
43
import { ServiceIdentifier } from '../../Type/Enum/index.js';
@@ -31,7 +30,7 @@ class PostChangePasswordEndpoint {
3130
return Promise.resolve()
3231
.then(() => {
3332
const url = this.fetchHelper.buildUrl(`/change-password`);
34-
this.logger.debug(`Executing HTTP POST request against url ${url} .`);
33+
this.logger.debug(`Executing HTTP POST request against URL: ${url}`);
3534
return fetch(
3635
url,
3736
this.fetchHelper.getDefaultPostOptions(
@@ -44,27 +43,9 @@ class PostChangePasswordEndpoint {
4443
),
4544
);
4645
})
47-
.catch((error) => {
48-
throw new NetworkError(`Experienced generic network error during creating resource.`, error);
49-
})
50-
.then(async (response: Response) => {
51-
if (response.ok && response.status === 204) {
52-
return;
53-
}
54-
const contentType = response.headers.get('Content-Type');
55-
if (contentType === null) {
56-
throw new ParseError('Response does not contain content type header.');
57-
}
58-
if (!contentType.includes('application/problem+json')) {
59-
throw new ParseError("Unable to parse response as content type is not 'application/problem+json'.");
60-
}
61-
const data = await response.json();
62-
throw this.fetchHelper.createResponseErrorFromBadResponse(response, data);
63-
})
64-
.catch((error) => {
65-
this.logger.error(error.message, error);
66-
throw error;
67-
});
46+
.catch((error) => this.fetchHelper.rethrowErrorAsNetworkError(error))
47+
.then((response) => this.fetchHelper.parseEmptyResponse(response))
48+
.catch((error) => this.fetchHelper.logAndThrowError(error));
6849
}
6950
}
7051

src/Endpoint/User/PostRegisterEndpoint.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { NetworkError, ParseError } from '../../Error/index.js';
21
import { FetchHelper, Logger, ServiceResolver } from '../../Service/index.js';
3-
import { Data, UniqueUserIdentifier, Uuid, validateUuidFromString } from '../../Type/Definition/index.js';
2+
import { Data, UniqueUserIdentifier, Uuid } from '../../Type/Definition/index.js';
43
import { ServiceIdentifier } from '../../Type/Enum/index.js';
54

65
/**
@@ -27,7 +26,7 @@ class PostRegisterEndpoint {
2726
return Promise.resolve()
2827
.then(() => {
2928
const url = this.fetchHelper.buildUrl(`/register`);
30-
this.logger.debug(`Executing HTTP POST request against url ${url} .`);
29+
this.logger.debug(`Executing HTTP POST request against URL: ${url}`);
3130
return fetch(
3231
url,
3332
this.fetchHelper.getDefaultPostOptions(
@@ -40,31 +39,9 @@ class PostRegisterEndpoint {
4039
),
4140
);
4241
})
43-
.catch((error) => {
44-
throw new NetworkError(`Experienced generic network error during creating resource.`, error);
45-
})
46-
.then(async (response: Response) => {
47-
if (response.ok && response.status === 201) {
48-
if (response.headers.has('Location')) {
49-
const location = response.headers.get('Location') as string;
50-
const rawUuid = location.split('/').at(-1) as string;
51-
return validateUuidFromString(rawUuid);
52-
}
53-
}
54-
const contentType = response.headers.get('Content-Type');
55-
if (contentType === null) {
56-
throw new ParseError('Response does not contain content type header.');
57-
}
58-
if (!contentType.includes('application/problem+json')) {
59-
throw new ParseError("Unable to parse response as content type is not 'application/problem+json'.");
60-
}
61-
const data = await response.json();
62-
throw this.fetchHelper.createResponseErrorFromBadResponse(response, data);
63-
})
64-
.catch((error) => {
65-
this.logger.error(error.message, error);
66-
throw error;
67-
});
42+
.catch((error) => this.fetchHelper.rethrowErrorAsNetworkError(error))
43+
.then((response) => this.fetchHelper.parseLocationResponse(response))
44+
.catch((error) => this.fetchHelper.logAndThrowError(error));
6845
}
6946
}
7047

src/Endpoint/User/PostTokenEndpoint.ts

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NetworkError, ParseError } from '../../Error/index.js';
21
import { FetchHelper, Logger, ServiceResolver, TokenParser } from '../../Service/index.js';
32
import { Data, Token, UniqueUserIdentifier } from '../../Type/Definition/index.js';
43
import { ServiceIdentifier } from '../../Type/Enum/index.js';
@@ -29,7 +28,7 @@ class PostTokenEndpoint {
2928
return Promise.resolve()
3029
.then(() => {
3130
const url = this.fetchHelper.buildUrl(`/token`);
32-
this.logger.debug(`Executing HTTP POST request against url ${url} .`);
31+
this.logger.debug(`Executing HTTP POST request against URL: ${url}`);
3332
return fetch(
3433
url,
3534
this.fetchHelper.getDefaultPostOptions(
@@ -42,32 +41,10 @@ class PostTokenEndpoint {
4241
),
4342
);
4443
})
45-
.catch((error) => {
46-
throw new NetworkError(`Experienced generic network error during creating resource.`, error);
47-
})
48-
.then(async (response: Response) => {
49-
const contentType = response.headers.get('Content-Type');
50-
if (contentType === null) {
51-
throw new ParseError('Response does not contain content type header.');
52-
}
53-
if (!(contentType.includes('application/json') || contentType.includes('application/problem+json'))) {
54-
throw new ParseError(
55-
"Unable to parse response as content type is neither 'application/json' nor 'application/problem+json'.",
56-
);
57-
}
58-
const data = await response.json();
59-
if (!response.ok) {
60-
throw this.fetchHelper.createResponseErrorFromBadResponse(response, data);
61-
}
62-
return data;
63-
})
64-
.then<Token>((jsonResponse) => {
65-
return this.tokenParser.rawTokenToToken(jsonResponse);
66-
})
67-
.catch((error) => {
68-
this.logger.error(error.message, error);
69-
throw error;
70-
});
44+
.catch((error) => this.fetchHelper.rethrowErrorAsNetworkError(error))
45+
.then((response) => this.fetchHelper.parseJsonResponse(response))
46+
.then((rawToken) => this.tokenParser.rawTokenToToken(rawToken))
47+
.catch((error) => this.fetchHelper.logAndThrowError(error));
7148
}
7249
}
7350

0 commit comments

Comments
 (0)