-
Notifications
You must be signed in to change notification settings - Fork 262
Expand file tree
/
Copy pathheaders.ts
More file actions
78 lines (70 loc) · 2.49 KB
/
Copy pathheaders.ts
File metadata and controls
78 lines (70 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {CLI_KIT_VERSION} from '../../../public/common/version.js'
import {firstPartyDev} from '../../../public/node/context/local.js'
import {AbortError} from '../../../public/node/error.js'
import https from 'https'
class RequestClientError extends AbortError {
statusCode: number
public constructor(message: string, statusCode: number) {
const tryMessage =
statusCode === 403
? 'Ensure you are using the correct account. You can switch with `shopify auth login`'
: undefined
super(message, tryMessage)
this.statusCode = statusCode
}
}
export class GraphQLClientError extends RequestClientError {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
errors?: any[]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public constructor(message: string, statusCode: number, errors?: any[]) {
super(message, statusCode)
this.errors = errors
this.stack = undefined
}
}
/**
* Removes the sensitive data from the headers and outputs them as a string.
* @param headers - HTTP headers.
* @returns A sanitized version of the headers as a string.
*/
export function sanitizedHeadersOutput(headers: Record<string, string>): string {
const sanitized: Record<string, string> = {}
const keywords = ['token', 'authorization', 'subject_token', 'cookie']
Object.keys(headers).forEach((header) => {
if (keywords.find((keyword) => header.toLocaleLowerCase().includes(keyword)) === undefined) {
sanitized[header] = headers[header]!
}
})
return Object.keys(sanitized)
.map((header) => {
return ` - ${header}: ${sanitized[header]}`
})
.join('\n')
}
export function buildHeaders(token?: string): Record<string, string> {
const userAgent = `Shopify CLI; v=${CLI_KIT_VERSION}`
const headers: Record<string, string> = {
'User-Agent': userAgent,
'Keep-Alive': 'timeout=30',
// 'Sec-CH-UA': secCHUA, This header requires the Git sha.
'Sec-CH-UA-PLATFORM': process.platform,
'Content-Type': 'application/json',
...(firstPartyDev() && {'X-Shopify-Cli-Employee': '1'}),
}
if (token) {
const authString = token.match(/^shp(at|ua|ca|tka)/) ? token : `Bearer ${token}`
headers.authorization = authString
headers['X-Shopify-Access-Token'] = authString
}
return headers
}
/**
* This utility function returns the https.Agent to use for a given service.
*/
export async function httpsAgent(): Promise<https.Agent> {
return new https.Agent({
rejectUnauthorized: true,
keepAlive: true,
})
}