Skip to content

Commit 24aa068

Browse files
authored
Merge pull request #88 from dunglas/url
Improve the generated code by using the URL object
2 parents 55ed9f2 + bd77215 commit 24aa068

File tree

5 files changed

+13
-26
lines changed

5 files changed

+13
-26
lines changed

src/generators/BaseGenerator.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from "fs";
22
import handlebars from "handlebars";
33
import mkdirp from "mkdirp";
44
import { sprintf } from "sprintf-js";
5-
import urlapi from "url";
65

76
export default class {
87
templates = {};
@@ -52,16 +51,8 @@ export default class {
5251
if (warn) console.log(`The file "${dest}" already exists`);
5352
}
5453

55-
createEntrypoint(apiEntry, dest) {
56-
const url = urlapi.parse(apiEntry);
57-
const { protocol, host, pathname } = url;
58-
59-
this.createFile(
60-
"_entrypoint.js",
61-
dest,
62-
{ host: `${protocol}//${host}`, path: pathname },
63-
false
64-
);
54+
createEntrypoint(entrypoint, dest) {
55+
this.createFile("_entrypoint.js", dest, { entrypoint }, false);
6556
}
6657

6758
getHtmlInputTypeFromField(field) {

templates/_entrypoint.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export const API_HOST = '{{{host}}}'
2-
export const API_PATH = '{{{path}}}'
1+
export const API_ENTRYPOINT = '{{{entrypoint}}}';

templates/react-common/utils/fetch.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { SubmissionError } from 'redux-form';
2-
import { API_HOST, API_PATH } from '../config/_entrypoint';
2+
import { API_ENTRYPOINT } from '../config/_entrypoint';
33

44
const jsonLdMimeType = 'application/ld+json';
55

6-
export default function (url, options = {}) {
6+
export default function (id, options = {}) {
77
if ('undefined' === typeof options.headers) options.headers = new Headers();
88
if (null === options.headers.get('Accept')) options.headers.set('Accept', jsonLdMimeType);
99

1010
if ('undefined' !== options.body && !(options.body instanceof FormData) && null === options.headers.get('Content-Type')) {
1111
options.headers.set('Content-Type', jsonLdMimeType);
1212
}
1313

14-
const link = url.includes(API_PATH) ? API_HOST + url : API_HOST + API_PATH + url;
15-
16-
return fetch(link, options).then(response => {
14+
return fetch(new URL(id, API_ENTRYPOINT).toString(), options).then(response => {
1715
if (response.ok) return response;
1816

1917
return response

templates/react/utils/helpers.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React from 'react';
22
import { Link } from 'react-router-dom';
3-
import { API_PATH } from '../config/_entrypoint';
3+
import { API_ENTRYPOINT } from '../config/_entrypoint';
44

55
export function itemToLinks(items) {
66
return Array.isArray(items) ? items.map(item => createLink(item)) : createLink(items);
77
}
88

99
function createLink(item) {
10-
if ('string' !== typeof(item) || !item.includes(API_PATH)) {
10+
const apiPathname = (new URL(API_ENTRYPOINT).pathname);
11+
if ('string' !== typeof(item) || !item.includes(apiPathname)) {
1112
return <div key={item}>{item}</div>;
1213
}
1314

14-
const routeWithoutPrefix = item.replace(API_PATH, '');
15+
const routeWithoutPrefix = item.replace(apiPathname, '');
1516
const splittedRoute = routeWithoutPrefix.split('/');
1617
const route = '/' === routeWithoutPrefix[0] ? splittedRoute[1] : splittedRoute[0];
1718

templates/vue/utils/fetch.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import SubmissionError from '../error/SubmissionError'
2-
import { API_HOST, API_PATH } from '../config/_entrypoint'
2+
import { API_ENTRYPOINT } from '../config/_entrypoint';
33

44
const jsonLdMimeType = 'application/ld+json'
55

6-
export default function (url, options = {}) {
6+
export default function (id, options = {}) {
77
if (typeof options.headers === 'undefined') Object.assign(options, { headers: new Headers() })
88

99
if (options.headers.get('Accept') === null) options.headers.set('Accept', jsonLdMimeType)
@@ -12,9 +12,7 @@ export default function (url, options = {}) {
1212
options.headers.set('Content-Type', jsonLdMimeType)
1313
}
1414

15-
const link = url.includes(API_PATH) ? API_HOST + url : API_HOST + API_PATH + url
16-
17-
return fetch(link, options).then((response) => {
15+
return fetch(new URL(id, API_ENTRYPOINT).toString(), options).then((response) => {
1816
if (response.ok) return response
1917

2018
return response

0 commit comments

Comments
 (0)