import * as Querystring from 'querystring' import { Model } from '@oyst/utils' import { ONECLICK_FRONT_URL } from '../lib/constants' import parseUrl from 'url-parse' export default class Url extends Model { private _token: string = null private _merchantID: string = null private _url: string = null private _version: number = null constructor (o?: Partial) { super() this.init(o) } public set token (value: string) { this._token = value } public get token (): string { return this._token } public set version (value: number) { this._version = value } public get version (): number { return this._version || 1 } public set merchantID (value: string) { this._merchantID = value } public get merchantID (): string { return this._merchantID } public set url (value: string) { this._url = value } public get url (): string { return this._url } public encode (): string { this._url = `${ONECLICK_FRONT_URL}?${Querystring.stringify({ m: this._merchantID, t: this._token, v: this.version })}` return this._url } public decode (): void { const parsed = parseUrl(this._url, true) this._merchantID = parsed.query.m this._token = parsed.query.t } }