Skip to content

Commit 84eccf5

Browse files
author
Phoebe Schmidt
authored
fix(proxy): allow proxy options in argv (#178)
<!-- Thank you for reporting an issue. Please fill in as much of the template below as you're able. Feel free to delete any section you want to skip. PLEASE **DO NOT** share any credentials related to your Contentful account like <space_id> or <access_token>. If this is an urgent issue you are having with Contentful It's better to contact [[email protected]](mailto:[email protected]). --> ## Summary Allows `proxy` and `rawProxy` in argv.
1 parent e2d5bb2 commit 84eccf5

File tree

5 files changed

+59
-32
lines changed

5 files changed

+59
-32
lines changed

index.d.ts

+25-23
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export interface Field {
6565

6666
}
6767

68-
type LinkMimetype = 'attachment' | 'plaintext' | 'image' | 'audio' | 'video' | 'richtext' |
68+
type LinkMimetype = 'attachment' | 'plaintext' | 'image' | 'audio' | 'video' | 'richtext' |
6969
'presentation' | 'spreadsheet' | 'pdfdocument' | 'archive' | 'code' | 'markup'
7070

7171
export interface IValidation {
@@ -103,7 +103,7 @@ export interface IEditorInterfaceOptions {
103103
/** (only for fields of type boolean) Shows this text next to the radio button that sets this value to true. Defaults to “Yes”. */
104104
trueLabel?: string
105105
/** (only for fields of type boolean) Shows this text next to the radio button that sets this value to false. Defaults to “No”. */
106-
falseLabel?: string
106+
falseLabel?: string
107107
/** (only for fields of type rating) Number of stars to select from. Defaults to 5. */
108108
stars?: number
109109
/** (only for fields of type datePicker) – One of "dateonly", "time", "timeZ" (default). Specifies whether to show the clock and/or timezone inputs. */
@@ -141,7 +141,7 @@ export interface ContentType {
141141

142142
/**
143143
* Changes the editor interface of given field's ID.
144-
*
144+
*
145145
* @param fieldId The ID of the field.
146146
* @param widgetId The new widget ID for the field.
147147
* @param settings Widget settings
@@ -150,14 +150,14 @@ export interface ContentType {
150150

151151
/**
152152
* Resets the editor interface of given field's ID.
153-
*
153+
*
154154
* @param fieldId The ID of the field.
155155
*/
156156
resetEditorInterface (fieldId: string): void
157157

158158
/**
159159
* copies the editor interface setting from a field to another field in the same content type.
160-
*
160+
*
161161
* @param sourceFieldId The ID of the field to copy the editorinterface setting from.
162162
* @param destinationFieldId The ID of the field to apply the copied editorinterface setting to.
163163
*/
@@ -219,30 +219,30 @@ export interface ITransformEntriesToTypeConfig {
219219
}
220220

221221
export interface IDeriveLinkedEntriesConfig {
222-
/**
223-
* (required) – Source content type ID
224-
*
222+
/**
223+
* (required) – Source content type ID
224+
*
225225
* This is the content type which has the 'from' fields
226226
*/
227227
contentType: string,
228-
/**
228+
/**
229229
* (required) – Target content type ID
230-
*
230+
*
231231
* This is the content type that the link points to
232232
*/
233233
derivedContentType: string,
234-
/**
234+
/**
235235
* (required) – Array of the source field IDs
236-
*
236+
*
237237
* The values in these fields on the source content type
238238
* will be given to "deriveEntryForLocale"
239239
*/
240240
from: string[],
241241
/** (required) – ID of the field on the source content type in which to insert the reference */
242242
toReferenceField: string,
243-
/**
243+
/**
244244
* (required) – Array of the field IDs on the target content type
245-
*
245+
*
246246
* The fields returned from "deriveEntryForLocale" will be written to these fields
247247
* on the new instance of the derived content type.
248248
*/
@@ -258,27 +258,27 @@ export interface IDeriveLinkedEntriesConfig {
258258
* (required) – Function that generates the field values for the derived entry.
259259
* fields is an object containing each of the from fields. Each field will contain their current localized values (i.e. fields == {myField: {'en-US': 'my field value'}})
260260
* locale one of the locales in the space being transformed
261-
*
261+
*
262262
* The return value must be an object with the same keys as specified in derivedFields. Their values will be written to the respective new entry fields for the current locale (i.e. {nameField: 'myNewValue'})
263263
*/
264264
deriveEntryForLocale: (inputFields: ContentFields, locale: string) => { [field: string]: any }
265265
}
266266

267-
/**
267+
/**
268268
* The main interface for creating and editing content types.
269269
*/
270270
export default interface Migration {
271271
/**
272272
* Creates a content type with provided id and returns a reference to the newly created content type.
273273
*
274274
* id : string – The ID of the content type.
275-
*
275+
*
276276
* opts : Object – Content type definition, with the following options:
277-
*
277+
*
278278
* * name : string – Name of the content type.
279279
* * description : string – Description of the content type.
280280
* * displayField : string – ID of the field to use as the display field for the content type.
281-
*
281+
*
282282
* @param id string – The ID of the content type.
283283
* @param init Object – Content type definition
284284
*/
@@ -299,27 +299,29 @@ export default interface Migration {
299299

300300
/**
301301
* For the given content type, transforms all its entries according to the user-provided transformEntryForLocale function. For each entry, the CLI will call this function once per locale in the space, passing in the from fields and the locale as arguments. The transform function is expected to return an object with the desired target fields. If it returns undefined, this entry locale will be left untouched.
302-
* @param transformation
302+
* @param transformation
303303
*/
304304
transformEntries (transformation: ITransformEntriesConfig): void
305305

306306
/**
307307
* For the given content type, transforms all its entries according to the user-provided transformEntryForLocale function into a new content type. For each entry, the CLI will call this function once per locale in the space, passing in the from fields and the locale as arguments. The transform function is expected to return an object with the desired target fields. If it returns undefined, this entry locale will be left untouched
308-
* @param transformation
308+
* @param transformation
309309
*/
310310
transformEntriesToType (transformation: ITransformEntriesToTypeConfig): void
311311

312312
/**
313313
* For each entry of the given content type (source entry), derives a new entry and sets up a reference to it on the source entry. The content of the new entry is generated by the user-provided deriveEntryForLocale function. For each source entry, this function will be called as many times as there are locales in the space. Each time, it will be called with the from fields and one of the locales as arguments. The derive function is expected to return an object with the desired target fields. If it returns undefined, the new entry will have no values for the current locale.
314-
* @param transformation
314+
* @param transformation
315315
*/
316316
deriveLinkedEntries (transformation: IDeriveLinkedEntriesConfig): void
317317
}
318318

319319
export interface ClientConfig {
320320
accessToken?: string
321321
spaceId?: string
322-
environmentId?: string
322+
environmentId?: string,
323+
proxy?: string,
324+
rawProxy?: boolean
323325
}
324326

325327
export type MakeRequest = (requestConfig: axios.AxiosRequestConfig) => axios.AxiosResponse['data']

src/bin/lib/config.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const configPath = path.resolve(homedir, '.contentfulrc.json')
88
interface ClientConfig {
99
accessToken?: string
1010
spaceId?: string
11-
environmentId?: string
11+
environmentId?: string,
12+
proxy?: string,
13+
rawProxy?: boolean
1214
}
1315

1416
function getFileConfig (): ClientConfig {
@@ -29,11 +31,13 @@ function getEnvConfig (): ClientConfig {
2931
{}
3032
}
3133

32-
function getArgvConfig ({spaceId, environmentId = 'master', accessToken}): ClientConfig {
34+
function getArgvConfig ({spaceId, environmentId = 'master', accessToken, proxy, rawProxy}): ClientConfig {
3335
const config = {
3436
spaceId,
3537
environmentId,
36-
accessToken
38+
accessToken,
39+
proxy,
40+
rawProxy
3741
}
3842

3943
if (!config.accessToken) {

src/bin/lib/contentful-client/index.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { loadProxyFromEnv, agentFromProxy } from './proxy'
1+
import { loadProxyFromEnv, agentFromProxy, proxyStringToObject } from './proxy'
22
// One cannot rewire const's
33
// eslint-disable-next-line
44
import { createClient as _createClient } from 'contentful-management'
@@ -9,7 +9,15 @@ function createManagementClient (params) {
99
throw new Error('Please specify the application name that uses this client instance')
1010
}
1111

12-
params.proxy = params.proxy || loadProxyFromEnv(process.env)
12+
const proxyFromParams = params.proxy && proxyStringToObject(params.proxy)
13+
if (proxyFromParams) {
14+
params.proxy = proxyFromParams
15+
} else {
16+
const proxyFromEnv = loadProxyFromEnv(process.env)
17+
if (proxyFromEnv) {
18+
params.proxy = proxyFromEnv
19+
}
20+
}
1321

1422
if (!params.rawProxy) {
1523
const { httpsAgent } = agentFromProxy(params.proxy)

src/bin/lib/contentful-client/proxy.ts

+11-4
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,20 @@ function agentFromProxy (proxy): { httpsAgent?: HttpsProxyAgent } {
7070
return {}
7171
}
7272

73-
const { host, port, auth: { username, password } } = proxy
74-
const auth = `${username}:${password}`
75-
const agent = new HttpsProxyAgent({ host, port, auth })
73+
const { host, port, auth } = proxy
74+
75+
let agentAuth
76+
if (auth) {
77+
const { username, password } = auth
78+
agentAuth = `${username}:${password}`
79+
}
80+
81+
const agent = new HttpsProxyAgent({ host, port, auth: agentAuth })
7682
return { httpsAgent: agent }
7783
}
7884

7985
export {
8086
loadProxyFromEnv,
81-
agentFromProxy
87+
agentFromProxy,
88+
proxyStringToObject
8289
}

test/unit/bin/lib/config.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ describe('Config', function () {
3030
const config = getConfig({accessToken: 'fooMyBar'})
3131
expect(config.accessToken).to.eql('fooMyBar')
3232
})
33+
34+
it('exposes proxy and rawProxy from argv', function () {
35+
const config = getConfig({ proxy: 'https://foo.bar:443', rawProxy: true })
36+
expect(config.proxy).to.eql('https://foo.bar:443')
37+
expect(config.rawProxy).to.eql(true)
38+
})
3339
})

0 commit comments

Comments
 (0)