From d1b7fbeeece0a0c6cfdb2ebddb2f6f75a33779f7 Mon Sep 17 00:00:00 2001 From: Sherwin H Date: Thu, 10 Oct 2019 14:52:41 -0700 Subject: [PATCH] replace .ts with .d.ts declaration file --- dist/imgix-core-js.d.ts | 27 +++++ {src => dist}/tsconfig.json | 9 +- package-lock.json | 6 +- package.json | 9 +- src/imgix-core-js.ts | 198 ------------------------------------ 5 files changed, 40 insertions(+), 209 deletions(-) create mode 100644 dist/imgix-core-js.d.ts rename {src => dist}/tsconfig.json (78%) delete mode 100644 src/imgix-core-js.ts diff --git a/dist/imgix-core-js.d.ts b/dist/imgix-core-js.d.ts new file mode 100644 index 00000000..92e3847f --- /dev/null +++ b/dist/imgix-core-js.d.ts @@ -0,0 +1,27 @@ +// Type definitions for imgix-core-js.js + +// export as namespace ImgixClient; + +/*~ This declaration specifies that the class constructor function + *~ is the exported object from the file + */ + +/*~ Write your module's methods and properties in this class */ +declare class ImgixClient { + domain: string; + useHTTPS: boolean; + includeLibraryParam: boolean; + secureURLToken: string; + + constructor(opts: {domain: string; secureURLToken?: string; useHTTPS?: boolean; includeLibraryParam?: boolean;}); + + + buildURL(path: string, params?: {}): string; + _sanitizePath(path: string): string; + _buildParams(params: {}): string; + _signParams(path: string, queryParams?: {}): string; + buildSrcSet(path: string, params?: {}): string; + _buildSrcSetPairs(path: string, params?: {}): string; + _buildDPRSrcSet(path: string, params?: {}): string; +} +export = ImgixClient; diff --git a/src/tsconfig.json b/dist/tsconfig.json similarity index 78% rename from src/tsconfig.json rename to dist/tsconfig.json index 365a0b77..2b6425ea 100644 --- a/src/tsconfig.json +++ b/dist/tsconfig.json @@ -2,9 +2,9 @@ "compilerOptions": { "outDir": "../dist", // "rootDir": "./src", - "allowJs": true, - "target": "es5", - "module": "CommonJS", + // "allowJs": true, + "target": "es2015", + "module": "umd", // types option has been previously configured "types": [ // add node as an option @@ -17,6 +17,9 @@ // "node_modules/@types" // ] }, + "files": [ + "imgix-core-js.d.ts" + ], "include": [ "./imgix-core-js.ts" ] diff --git a/package-lock.json b/package-lock.json index 95e5f3b6..b750e233 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,8 +43,7 @@ "@types/node": { "version": "12.7.8", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.8.tgz", - "integrity": "sha512-FMdVn84tJJdV+xe+53sYiZS4R5yn1mAIxfj+DVoNiQjTYz1+OYmjwEZr1ev9nU0axXwda0QDbYl06QHanRVH3A==", - "dev": true + "integrity": "sha512-FMdVn84tJJdV+xe+53sYiZS4R5yn1mAIxfj+DVoNiQjTYz1+OYmjwEZr1ev9nU0axXwda0QDbYl06QHanRVH3A==" }, "ansi-colors": { "version": "3.2.3", @@ -1553,8 +1552,7 @@ "typescript": { "version": "3.6.3", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.6.3.tgz", - "integrity": "sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==", - "dev": true + "integrity": "sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==" }, "uglify-js": { "version": "3.6.0", diff --git a/package.json b/package.json index c0fe1ca8..b574d832 100644 --- a/package.json +++ b/package.json @@ -5,22 +5,23 @@ "repository": "https://github.com/imgix/imgix-core-js", "scripts": { "assert_version": "node assert_version.js", - "compile": "mkdir -p dist && uglifyjs ./src/imgix-core-js.js -o ./dist/imgix-core-js.min.js && tsc -p src", + "compile": "mkdir -p dist && uglifyjs ./src/imgix-core-js.js -o ./dist/imgix-core-js.min.js && tsc -p dist", "prepublish": "npm run compile && npm run assert_version", "test": "mocha" }, "main": "dist/imgix-core-js.js", + "types": "dist/imgix-core-js.d.ts", "dependencies": { "js-base64": "^2.1.9", - "md5": "^2.2.1" + "md5": "^2.2.1", + "typescript": "^3.6.3", + "@types/node": "^12.7.8" }, "license": "BSD-2-Clause", "devDependencies": { - "@types/node": "^12.7.8", "mocha": "6.2.1", "phantomjs": "2.1.7", "sinon": "7.5.0", - "typescript": "^3.6.3", "uglify-js": "3.6.0" } } diff --git a/src/imgix-core-js.ts b/src/imgix-core-js.ts deleted file mode 100644 index 3da9ad34..00000000 --- a/src/imgix-core-js.ts +++ /dev/null @@ -1,198 +0,0 @@ -declare var define: any -(function (global, factory) { - if (typeof define === 'function' && define.amd) { - define('Imgix', ['exports', 'md5', 'js-base64'], factory); - } else if (typeof exports !== 'undefined') { - module.exports = factory(exports, require('md5'), require('js-base64').Base64); - } else { - var mod = { - exports: {} - }; - global.ImgixClient = factory(mod.exports, global.md5, global.Base64); - } -})(this, function (exports, _md5, _jsBase64) { - var md5 = _md5; - var Base64 = _jsBase64.Base64 || _jsBase64; - - // package version used in the ix-lib parameter - var VERSION = '2.1.2'; - // regex pattern used to determine if a domain is valid - var DOMAIN_REGEX = /^(?:[a-z\d\-_]{1,62}\.){0,125}(?:[a-z\d](?:\-(?=\-*[a-z\d])|[a-z]|\d){0,62}\.)[a-z\d]{1,63}$/i; - // returns an array of width values used during scrset generation - var TARGET_WIDTHS = (function() { - var resolutions = []; - var prev = 100; - var INCREMENT_PERCENTAGE = 8; - var MAX_SIZE = 8192; - - var ensureEven = function(n){ - return 2 * Math.round(n / 2); - }; - - while (prev <= MAX_SIZE) { - resolutions.push(ensureEven(prev)); - prev *= 1 + (INCREMENT_PERCENTAGE / 100) * 2; - } - - resolutions.push(MAX_SIZE); - return resolutions; - })(); - // default ImgixClient settings passed in during instantiation - var DEFAULTS = { - domain: String = null, - useHTTPS: true, - includeLibraryParam: true - }; - - var ImgixClient = (function() { - function ImgixClient(opts: object | {}) { - var key, val; - - this.settings = {}; - - for (key in DEFAULTS) { - val = DEFAULTS[key]; - this.settings[key] = val; - } - - for (key in opts) { - val = opts[key]; - this.settings[key] = val; - } - - if (typeof this.settings.domain != "string") { - throw new Error('ImgixClient must be passed a valid string domain'); - } - - if (DOMAIN_REGEX.exec(this.settings.domain) == null) { - throw new Error( - 'Domain must be passed in as fully-qualified ' + - 'domain name and should not include a protocol or any path ' + - 'element, i.e. "example.imgix.net".'); - } - - if (this.settings.includeLibraryParam) { - this.settings.libraryParam = "js-" + VERSION; - } - - this.settings.urlPrefix = this.settings.useHTTPS ? 'https://' : 'http://' - } - - ImgixClient.prototype.buildURL = function(path: string, params: object | {}): string { - path = this._sanitizePath(path); - - if (params == null) { - params = {}; - } - - var queryParams = this._buildParams(params); - if (!!this.settings.secureURLToken) { - queryParams = this._signParams(path, queryParams); - } - - return this.settings.urlPrefix + this.settings.domain + path + queryParams; - }; - - ImgixClient.prototype._sanitizePath = function(path: string): string { - // Strip leading slash first (we'll re-add after encoding) - path = path.replace(/^\//, ''); - - if (/^https?:\/\//.test(path)) { - // Use de/encodeURIComponent to ensure *all* characters are handled, - // since it's being used as a path - path = encodeURIComponent(path); - } else { - // Use de/encodeURI if we think the path is just a path, - // so it leaves legal characters like '/' and '@' alone - path = encodeURI(path); - } - - return '/' + path; - }; - - ImgixClient.prototype._buildParams = function(params: {}): string { - if (this.settings.libraryParam) { - params['ixlib'] = this.settings.libraryParam - } - - var queryParams = []; - var key, val, encodedKey, encodedVal; - for (key in params) { - val = params[key]; - encodedKey = encodeURIComponent(key); - encodedVal; - - if (key.substr(-2) === '64') { - encodedVal = Base64.encodeURI(val); - } else { - encodedVal = encodeURIComponent(val); - } - queryParams.push(encodedKey + "=" + encodedVal); - } - - if (queryParams[0]) { - queryParams[0] = "?" + queryParams[0]; - } - console.log(typeof queryParams.join('&')); - return queryParams.join('&'); - }; - - ImgixClient.prototype._signParams = function(path: string, queryParams: any): string { - var signatureBase = this.settings.secureURLToken + path + queryParams; - var signature = md5(signatureBase); - - if (queryParams.length > 0) { - return queryParams = queryParams + "&s=" + signature; - } else { - return queryParams = "?s=" + signature; - } - }; - - ImgixClient.prototype.buildSrcSet = function (path: string, params: any): string { - var params = params || {}; - var width = params.w; - var height = params.h; - var aspectRatio = params.ar; - - if ((width) || (height && aspectRatio)) { - return this._buildDPRSrcSet(path, params); - } - else { - return this._buildSrcSetPairs(path, params); - } - }; - - ImgixClient.prototype._buildSrcSetPairs = function(path: string, params: any): string { - var srcset = ''; - var currentWidth; - - for(var i = 0; i < TARGET_WIDTHS.length; i++) { - currentWidth = TARGET_WIDTHS[i]; - params.w = currentWidth; - srcset += this.buildURL(path, params) + ' ' + currentWidth + 'w,\n'; - } - - return srcset.slice(0,-2); - }; - - ImgixClient.prototype._buildDPRSrcSet = function(path: string, params: any): string { - var srcset = ''; - var targetRatios = [1, 2, 3, 4, 5]; - var currentRatio; - - for(var i = 0; i < targetRatios.length; i++) { - currentRatio = targetRatios[i]; - params.dpr = currentRatio; - srcset += this.buildURL(path, params) + ' ' + currentRatio + 'x,\n' - } - - return srcset.slice(0,-2); - }; - - ImgixClient.VERSION = VERSION; - - return ImgixClient; - })(); - - return ImgixClient; -});