|
| 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 | +} |
0 commit comments