1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- const queryUtils = require('query-string');
- class FileRequest {
- /**
- * @param {string} request
- */
- constructor(request) {
- const { file, query } = FileRequest.parse(request);
- this.file = file;
- this.query = query;
- }
- /**
- * @param {string} request
- * @return {{file: string, query: Object}}
- */
- static parse(request) {
- const parts = request.split('?');
- const file = parts[0];
- const query = parts[1] ? queryUtils.parse(parts[1]) : null;
- return { file, query };
- }
- /**
- * @return {string}
- */
- toString() {
- const { file, query } = this;
- const queryEncoded = query ? `?${queryUtils.stringify(query)}` : '';
- return `${file}${queryEncoded}`;
- }
- /**
- * @return {string}
- */
- stringify() {
- return this.toString();
- }
- /**
- * @return {string}
- */
- stringifyQuery() {
- return queryUtils.stringify(this.query);
- }
- /**
- * @param {FileRequest} request
- * @return {boolean}
- */
- equals(request) {
- if (!(request instanceof FileRequest)) {
- throw TypeError('request should be instance of FileRequest');
- }
- return this.toString() === request.toString();
- }
- /**
- * @param {FileRequest} request
- * @return {boolean}
- */
- fileEquals(request) {
- return this.file === request.file;
- }
- /**
- * @param {FileRequest} request
- * @return {boolean}
- */
- queryEquals(request) {
- return this.stringifyQuery() === request.stringifyQuery();
- }
- /**
- * @param {string} param
- * @return {boolean}
- */
- hasParam(param) {
- return this.query && param in this.query;
- }
- /**
- * @param {string} param
- * @return {string|null}
- */
- getParam(param) {
- return this.hasParam(param) ? this.query[param] : null;
- }
- }
- module.exports = FileRequest;
|