|
1 | | -import { NetworkError, ParseError } from '../../Error/index.js'; |
2 | | -import { FetchHelper, Logger } from '../../Service/index.js'; |
3 | | -import { |
4 | | - NodeWithOptionalId, |
5 | | - RelationWithOptionalId, |
6 | | - Uuid, |
7 | | - validateUuidFromString, |
8 | | -} from '../../Type/Definition/index.js'; |
| 1 | +import { FetchHelper, Logger, ServiceResolver } from '../../Service/index.js'; |
| 2 | +import { NodeWithOptionalId, RelationWithOptionalId, Uuid } from '../../Type/Definition/index.js'; |
| 3 | +import { ServiceIdentifier } from '../../Type/Enum/index.js'; |
9 | 4 |
|
10 | 5 | /** |
11 | 6 | * The post index endpoint creates a single element. |
12 | 7 | * |
13 | | - * **⚠️ Warning**: This is an internal class. You should not use it directly. |
14 | | - * |
15 | 8 | * @see [Further documentation](https://ember-nexus.github.io/web-sdk/#/endpoints/element?id=postindexendpoint) |
16 | 9 | * @see [Ember Nexus API: Create Root Level Element Endpoint](https://ember-nexus.github.io/api/#/api-endpoints/element/post-index) |
17 | | - * |
18 | | - * @internal |
19 | 10 | */ |
20 | | - |
21 | 11 | class PostIndexEndpoint { |
| 12 | + static identifier: ServiceIdentifier = ServiceIdentifier.endpointElementPostIndexEndpoint; |
22 | 13 | constructor( |
23 | 14 | private logger: Logger, |
24 | 15 | private fetchHelper: FetchHelper, |
25 | 16 | ) {} |
26 | 17 |
|
| 18 | + static constructFromServiceResolver(serviceResolver: ServiceResolver): PostIndexEndpoint { |
| 19 | + return new PostIndexEndpoint( |
| 20 | + serviceResolver.getServiceOrFail<Logger>(ServiceIdentifier.logger), |
| 21 | + serviceResolver.getServiceOrFail<FetchHelper>(ServiceIdentifier.fetchHelper), |
| 22 | + ); |
| 23 | + } |
| 24 | + |
27 | 25 | postIndex(element: NodeWithOptionalId | RelationWithOptionalId): Promise<Uuid> { |
28 | 26 | return Promise.resolve() |
29 | 27 | .then(() => { |
30 | 28 | const url = this.fetchHelper.buildUrl(`/`); |
31 | | - this.logger.debug(`Executing HTTP POST request against url ${url} .`); |
| 29 | + this.logger.debug(`Executing HTTP POST request against URL: ${url}`); |
32 | 30 | return fetch(url, this.fetchHelper.getDefaultPostOptions(JSON.stringify(element))); |
33 | 31 | }) |
34 | | - .catch((error) => { |
35 | | - throw new NetworkError(`Experienced generic network error during creating resource.`, error); |
36 | | - }) |
37 | | - .then(async (response: Response) => { |
38 | | - if (response.ok && response.status === 204) { |
39 | | - if (response.headers.has('Location')) { |
40 | | - const location = response.headers.get('Location') as string; |
41 | | - const rawUuid = location.split('/').at(-1) as string; |
42 | | - return validateUuidFromString(rawUuid); |
43 | | - } |
44 | | - } |
45 | | - const contentType = response.headers.get('Content-Type'); |
46 | | - if (contentType === null) { |
47 | | - throw new ParseError('Response does not contain content type header.'); |
48 | | - } |
49 | | - if (!contentType.includes('application/problem+json')) { |
50 | | - throw new ParseError("Unable to parse response as content type is not 'application/problem+json'."); |
51 | | - } |
52 | | - const data = await response.json(); |
53 | | - throw this.fetchHelper.createResponseErrorFromBadResponse(response, data); |
54 | | - }) |
55 | | - .catch((error) => { |
56 | | - this.logger.error(error.message, error); |
57 | | - throw error; |
58 | | - }); |
| 32 | + .catch((error) => this.fetchHelper.rethrowErrorAsNetworkError(error)) |
| 33 | + .then((response) => this.fetchHelper.parseLocationResponse(response)) |
| 34 | + .catch((error) => this.fetchHelper.logAndThrowError(error)); |
59 | 35 | } |
60 | 36 | } |
61 | 37 |
|
|
0 commit comments