@stuntman/shared 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/config.js CHANGED
@@ -35,6 +35,7 @@ try {
35
35
  configFromFile = config_1.default.get('stuntman');
36
36
  }
37
37
  catch (error) {
38
- console.warn('unable to find correct config');
38
+ // eslint-disable-next-line no-console
39
+ console.warn('unable to find correct config - starting with defaults');
39
40
  }
40
41
  exports.serverConfig = (0, defaults_1.default)(configFromFile, defaultConfig);
package/dist/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export type RuleMatchResult = boolean | {
28
28
  result: boolean;
29
29
  enableRuleIds?: string[];
30
30
  disableRuleIds?: string[];
31
+ description?: string;
31
32
  };
32
33
  export type RemotableFunction<T extends Function> = {
33
34
  localFn: T;
@@ -151,3 +152,12 @@ export type ServerConfig = {
151
152
  traffic: StorageConfig;
152
153
  };
153
154
  };
155
+ export interface RuleExecutorInterface {
156
+ addRule: (rule: Rule, overwrite?: boolean) => Promise<LiveRule>;
157
+ removeRule: (id: string) => Promise<void>;
158
+ enableRule: (id: string) => void;
159
+ disableRule: (id: string) => void;
160
+ findMatchingRule: (request: Request) => Promise<LiveRule | null>;
161
+ getRules: () => Promise<readonly LiveRule[]>;
162
+ getRule: (id: string) => Promise<LiveRule | undefined>;
163
+ }
@@ -7,5 +7,6 @@ export declare class RawHeaders extends Array<string> implements Stuntman.RawHea
7
7
  remove(name: string): void;
8
8
  toHeaderPairs(): readonly [string, string][];
9
9
  static fromHeaderPairs(headerPairs: [string, string][]): RawHeaders;
10
+ static fromHeadersRecord(headersRecord: Record<string, string | string[] | undefined>): RawHeaders;
10
11
  static toHeaderPairs(rawHeaders: string[]): readonly [string, string][];
11
12
  }
@@ -59,6 +59,19 @@ class RawHeaders extends Array {
59
59
  static fromHeaderPairs(headerPairs) {
60
60
  return new RawHeaders(...headerPairs.flatMap((x) => x));
61
61
  }
62
+ static fromHeadersRecord(headersRecord) {
63
+ const output = new RawHeaders();
64
+ for (const [key, value] of Object.entries(headersRecord)) {
65
+ if (typeof value === 'string' || value === undefined) {
66
+ output.add(key, value !== null && value !== void 0 ? value : '');
67
+ continue;
68
+ }
69
+ for (const subValue of value) {
70
+ output.add(key, subValue);
71
+ }
72
+ }
73
+ return output;
74
+ }
62
75
  static toHeaderPairs(rawHeaders) {
63
76
  const headers = new Array();
64
77
  for (let headerIndex = 0; headerIndex < rawHeaders.length; headerIndex += 2) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stuntman/shared",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Stuntman - HTTP proxy / mock shared types and utils",
5
5
  "main": "dist/index.js",
6
6
  "repository": {
package/src/config.ts CHANGED
@@ -42,7 +42,8 @@ let configFromFile: RecursivePartial<ServerConfig> = {};
42
42
  try {
43
43
  configFromFile = config.get<RecursivePartial<ServerConfig>>('stuntman');
44
44
  } catch (error) {
45
- console.warn('unable to find correct config');
45
+ // eslint-disable-next-line no-console
46
+ console.warn('unable to find correct config - starting with defaults');
46
47
  }
47
48
 
48
49
  export const serverConfig = defaults(configFromFile, defaultConfig) as ServerConfig;
package/src/index.ts CHANGED
@@ -42,7 +42,9 @@ export enum HttpCode {
42
42
 
43
43
  export type LocalVariables = Record<string, SerializableTypes>;
44
44
 
45
- export type RuleMatchResult = boolean | { result: boolean; enableRuleIds?: string[]; disableRuleIds?: string[] };
45
+ export type RuleMatchResult =
46
+ | boolean
47
+ | { result: boolean; enableRuleIds?: string[]; disableRuleIds?: string[]; description?: string };
46
48
 
47
49
  export type RemotableFunction<T extends Function> = {
48
50
  localFn: T;
@@ -197,3 +199,13 @@ export type ServerConfig = {
197
199
  traffic: StorageConfig;
198
200
  };
199
201
  };
202
+
203
+ export interface RuleExecutorInterface {
204
+ addRule: (rule: Rule, overwrite?: boolean) => Promise<LiveRule>;
205
+ removeRule: (id: string) => Promise<void>;
206
+ enableRule: (id: string) => void;
207
+ disableRule: (id: string) => void;
208
+ findMatchingRule: (request: Request) => Promise<LiveRule | null>;
209
+ getRules: () => Promise<readonly LiveRule[]>;
210
+ getRule: (id: string) => Promise<LiveRule | undefined>;
211
+ }
package/src/rawHeaders.ts CHANGED
@@ -65,6 +65,20 @@ export class RawHeaders extends Array<string> implements Stuntman.RawHeadersInte
65
65
  return new RawHeaders(...headerPairs.flatMap((x) => x));
66
66
  }
67
67
 
68
+ static fromHeadersRecord(headersRecord: Record<string, string | string[] | undefined>): RawHeaders {
69
+ const output = new RawHeaders();
70
+ for (const [key, value] of Object.entries(headersRecord)) {
71
+ if (typeof value === 'string' || value === undefined) {
72
+ output.add(key, value ?? '');
73
+ continue;
74
+ }
75
+ for (const subValue of value) {
76
+ output.add(key, subValue);
77
+ }
78
+ }
79
+ return output;
80
+ }
81
+
68
82
  static toHeaderPairs(rawHeaders: string[]): readonly [string, string][] {
69
83
  const headers = new Array<[string, string]>();
70
84
  for (let headerIndex = 0; headerIndex < rawHeaders.length; headerIndex += 2) {