Skip to content

Commit 896cd5f

Browse files
add new config option request headers
1 parent a3a2b3f commit 896cd5f

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

src/lib/ctx.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export default (options: Record<string, string>): Context => {
112112
ignoreHTTPSErrors: config.ignoreHTTPSErrors ?? false,
113113
skipBuildCreation: config.skipBuildCreation ?? false,
114114
tunnel: tunnelObj,
115-
userAgent: config.userAgent || ''
115+
userAgent: config.userAgent || '',
116+
requestHeaders: config.requestHeaders || {}
116117
},
117118
uploadFilePath: '',
118119
webStaticConfig: [],

src/lib/processSnapshot.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export async function prepareSnapshot(snapshot: Snapshot, ctx: Context): Promise
1919
if (ctx.config.basicAuthorization) {
2020
processedOptions.basicAuthorization = ctx.config.basicAuthorization;
2121
}
22+
if (ctx.config.requestHeaders && Array.isArray(ctx.config.requestHeaders)) {
23+
processedOptions.requestHeaders = ctx.config.requestHeaders
24+
}
2225
ctx.config.allowedHostnames.push(new URL(snapshot.url).hostname);
2326
processedOptions.allowedHostnames = ctx.config.allowedHostnames;
2427
processedOptions.skipCapturedCookies = ctx.env.SMARTUI_DO_NOT_USE_CAPTURED_COOKIES;
@@ -266,6 +269,11 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
266269
let token = Buffer.from(`${ctx.config.basicAuthorization.username}:${ctx.config.basicAuthorization.password}`).toString('base64');
267270
requestOptions.headers.Authorization = `Basic ${token}`;
268271
}
272+
if (ctx.config.requestHeaders && Array.isArray(ctx.config.requestHeaders)) {
273+
ctx.config.requestHeaders.forEach((header: { key: string, value: string }) => {
274+
requestOptions.headers[header.key] = header.value;
275+
});
276+
}
269277

270278
// get response
271279
let response, body;

src/lib/schemaValidation.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Snapshot, WebStaticConfig, FigmaDesignConfig } from '../types.js'
22
import Ajv, { JSONSchemaType } from 'ajv'
33
import addErrors from 'ajv-errors'
44
import constants from './constants.js'
5+
import { request } from 'http';
56

67
const ajv = new Ajv({ allErrors: true });
78
ajv.addFormat('web-url', {
@@ -161,7 +162,6 @@ const ConfigSchema = {
161162
errorMessage: {
162163
uniqueItems: "Invalid config; duplicates in allowedAssets"
163164
}
164-
165165
},
166166
basicAuthorization: {
167167
type: "object",
@@ -248,6 +248,26 @@ const ConfigSchema = {
248248
type: "string",
249249
errorMessage: "User Agent value must be a valid string"
250250
},
251+
requestHeaders: {
252+
type: "array",
253+
items: {
254+
type: "object",
255+
properties: {
256+
key: {
257+
type: "string",
258+
errorMessage: "Invalid config; key is mandatory"
259+
},
260+
value: {
261+
type: "string",
262+
errorMessage: "Invalid config; value is mandatory"
263+
},
264+
}
265+
},
266+
uniqueItems: true,
267+
errorMessage: {
268+
uniqueItems: "Invalid config; duplicates in requestHeaders"
269+
}
270+
}
251271
},
252272
anyOf: [
253273
{ required: ["web"] },

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export interface Context {
3535
skipBuildCreation?: boolean;
3636
tunnel: tunnelConfig | undefined;
3737
userAgent?: string;
38+
requestHeaders?: Array<reqHeaders>;
3839
};
3940
uploadFilePath: string;
4041
webStaticConfig: WebStaticConfig;
@@ -207,6 +208,11 @@ export interface basicAuth {
207208
password: string;
208209
}
209210

211+
export interface reqHeaders {
212+
key: string;
213+
value: string;
214+
}
215+
210216
export interface tunnelConfig {
211217
type: string;
212218
tunnelName: string;

0 commit comments

Comments
 (0)