Skip to content

Commit 0ccfd08

Browse files
committed
chore: renaming interfaces
1 parent d1f6450 commit 0ccfd08

3 files changed

Lines changed: 147 additions & 2 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
3+
import { Header } from '../headers';
4+
// import { QueryParam, setUrlParser, Url, UrlMatchPattern } from '../urls';
5+
6+
describe('test Header object', () => {
7+
it('test basic operations', () => {
8+
// const header = new Header('Content-Type: application/json\nUser-Agent: MyClientLibrary/2.0\n');
9+
const headerStr = 'Content-Type: application/json\nUser-Agent: MyClientLibrary/2.0\n';
10+
const headerObjs = [
11+
{ key: 'Content-Type', value: 'application/json' },
12+
{ key: 'User-Agent', value: 'MyClientLibrary/2.0' },
13+
];
14+
15+
expect(Header.parse(headerStr)).toEqual(headerObjs);
16+
expect(
17+
Header.parse(Header.unparse(headerObjs))
18+
).toEqual(headerObjs);
19+
});
20+
});
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import { unsupportedError } from './insomnia';
2+
import { Property, PropertyList } from './properties';
3+
4+
export interface HeaderDefinition {
5+
key: string;
6+
value: string;
7+
id?: string;
8+
name?: string;
9+
type?: string;
10+
disabled?: boolean;
11+
}
12+
13+
export class Header extends Property {
14+
_kind: string = 'Header';
15+
type: string = '';
16+
key: string;
17+
value: string;
18+
19+
constructor(
20+
opts: HeaderDefinition | string,
21+
name?: string, // if it is defined, it overrides 'key' (not 'name')
22+
) {
23+
super();
24+
25+
if (typeof opts === 'string') {
26+
const obj = Header.parseSingle(opts);
27+
this.key = obj.key;
28+
this.value = obj.value;
29+
} else {
30+
this.id = opts.id ? opts.id : '';
31+
this.key = opts.key ? opts.key : '';
32+
this.name = name ? name : (opts.name ? opts.name : '');
33+
this.value = opts.value ? opts.value : '';
34+
this.type = opts.type ? opts.type : '';
35+
this.disabled = opts ? opts.disabled : false;
36+
}
37+
}
38+
39+
static create(input?: { key: string; value: string } | string, name?: string): Header {
40+
return new Header(input || { key: '', value: '' }, name);
41+
}
42+
43+
static isHeader(obj: object) {
44+
return '_kind' in obj && obj._kind === 'Header';
45+
}
46+
47+
// example: 'Content-Type: application/json\nUser-Agent: MyClientLibrary/2.0\n'
48+
static parse(headerString: string): { key: string; value: string }[] {
49+
return headerString
50+
.split('\n')
51+
.filter(kvPart => kvPart.trim() !== '')
52+
.map(kvPart => Header.parseSingle(kvPart));
53+
}
54+
55+
static parseSingle(headerStr: string): { key: string; value: string } {
56+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers
57+
// the first colon is the separator
58+
const separatorPos = headerStr.indexOf(':');
59+
60+
if (separatorPos <= 0) {
61+
throw Error('Header.parseSingle: the header string seems invalid');
62+
}
63+
64+
const key = headerStr.slice(0, separatorPos);
65+
const value = headerStr.slice(separatorPos + 1);
66+
67+
return { key: key.trim(), value: value.trim() };
68+
}
69+
70+
static unparse(headers: { key: string; value: string }[] | PropertyList<Header>, separator?: string): string {
71+
const headerArray: { key: string; value: string }[] = [
72+
...headers.map(
73+
header => this.unparseSingle(header), {}
74+
),
75+
];
76+
77+
return headerArray.join(separator || '\n');
78+
}
79+
80+
static unparseSingle(header: { key: string; value: string } | Header): string {
81+
// both PropertyList and object contains 'key' and 'value'
82+
return `${header.key}: ${header.value}`;
83+
}
84+
85+
update(newHeader: { key: string; value: string }) {
86+
this.key = newHeader.key;
87+
this.value = newHeader.value;
88+
}
89+
90+
valueOf() {
91+
return this.value;
92+
}
93+
}
94+
95+
export class HeaderList<T extends Header> extends PropertyList<T> {
96+
constructor(parent: PropertyList<T> | undefined, populate: T[]) {
97+
super(
98+
Header,
99+
undefined,
100+
populate
101+
);
102+
this.parent = parent;
103+
}
104+
105+
static isHeaderList(obj: any) {
106+
return '_kind' in obj && obj._kind === 'HeaderList';
107+
}
108+
109+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
110+
eachParent(_iterator: any, _context?: object | undefined) {
111+
throw unsupportedError('eachParent');
112+
}
113+
114+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
115+
toObject(_excludeDisabled?: boolean, _caseSensitive?: boolean, _multiValue?: boolean, _sanitizeKeys?: boolean) {
116+
throw unsupportedError('toObject');
117+
}
118+
119+
contentSize(): number {
120+
return this.list
121+
.map(header => header.toString())
122+
.map(headerStr => headerStr.length) // TODO: handle special characters
123+
.reduce((totalSize, headerSize) => totalSize + headerSize, 0);
124+
}
125+
}

packages/insomnia/src/sdk/objects/properties.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ export class PropertyList<T extends Property> {
184184
protected list: T[] = [];
185185

186186
constructor(
187-
protected readonly _typeClass: {}, // TODO: it is not used before collection is introduced
188-
protected readonly parent: Property | PropertyList<any> | undefined,
187+
protected _typeClass: {}, // TODO: it is not used before collection is introduced
188+
protected parent: Property | PropertyList<any> | undefined,
189189
populate: T[],
190190
) {
191191
this.parent = parent;

0 commit comments

Comments
 (0)