@lhci/utils
Advanced tools
Comparing version 0.3.9 to 0.3.10
{ | ||
"name": "@lhci/utils", | ||
"version": "0.3.9", | ||
"version": "0.3.10", | ||
"license": "Apache-2.0", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
}, | ||
"gitHead": "b5367a74d973da2a9b60a7251797d3dd2421d1b7" | ||
"gitHead": "8b0fea7c80b52d7f7f64bde721a1aeb1cd9e319a" | ||
} |
@@ -17,2 +17,3 @@ /** | ||
this._rootURL = options.rootURL; | ||
/** @type {Record<string, string>} */ | ||
this._extraHeaders = options.extraHeaders || {}; | ||
@@ -26,2 +27,7 @@ this._fetch = options.fetch || fetch; | ||
/** @param {string|undefined} token */ | ||
setAdminToken(token) { | ||
this._extraHeaders = {...this._extraHeaders, 'x-lhci-admin-token': token || ''}; | ||
} | ||
/** | ||
@@ -63,3 +69,2 @@ * @param {string} url | ||
if (response.status === 204) return; | ||
const json = await response.json(); | ||
@@ -123,4 +128,17 @@ return json; | ||
/** | ||
* @param {string} rawUrl | ||
* @return {Promise<void>} | ||
*/ | ||
async _delete(rawUrl) { | ||
const headers = {...this._extraHeaders}; | ||
const response = await this._fetch(this._normalizeURL(rawUrl).href, { | ||
method: 'DELETE', | ||
headers, | ||
}); | ||
return this._convertFetchResponseToReturnValue(response); | ||
} | ||
/** | ||
* @return {Promise<void>} | ||
*/ | ||
async initialize() { | ||
@@ -180,3 +198,3 @@ throw new Error('Unimplemented'); | ||
/** | ||
* @param {StrictOmit<LHCI.ServerCommand.Project, 'id'|'token'>} unsavedProject | ||
* @param {StrictOmit<LHCI.ServerCommand.Project, 'id'|'token'|'adminToken'>} unsavedProject | ||
* @return {Promise<LHCI.ServerCommand.Project>} | ||
@@ -190,2 +208,10 @@ */ | ||
* @param {string} projectId | ||
* @return {Promise<void>} | ||
*/ | ||
async deleteProject(projectId) { | ||
return this._delete(`/v1/projects/${projectId}`); | ||
} | ||
/** | ||
* @param {string} projectId | ||
* @param {LHCI.ServerCommand.GetBuildsOptions} [options] | ||
@@ -227,2 +253,11 @@ * @return {Promise<LHCI.ServerCommand.Build[]>} | ||
* @param {string} buildId | ||
* @return {Promise<void>} | ||
*/ | ||
async deleteBuild(projectId, buildId) { | ||
return this._delete(`/v1/projects/${projectId}/builds/${buildId}`); | ||
} | ||
/** | ||
* @param {string} projectId | ||
* @param {string} buildId | ||
* @return {Promise<LHCI.ServerCommand.Build | undefined>} | ||
@@ -283,4 +318,3 @@ */ | ||
/** | ||
* @protected | ||
* @param {StrictOmit<LHCI.ServerCommand.Project, 'id'|'token'>} unsavedProject | ||
* @param {StrictOmit<LHCI.ServerCommand.Project, 'id'|'token'|'adminToken'>} unsavedProject | ||
* @return {Promise<LHCI.ServerCommand.Project>} | ||
@@ -294,3 +328,2 @@ */ | ||
/** | ||
* @protected | ||
* @param {StrictOmit<LHCI.ServerCommand.Statistic, 'id'>} unsavedStatistic | ||
@@ -324,2 +357,11 @@ * @return {Promise<LHCI.ServerCommand.Statistic>} | ||
/** | ||
* @param {string} projectId | ||
* @return {Promise<string>} | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
async _resetAdminToken(projectId) { | ||
throw new Error('Unimplemented'); | ||
} | ||
async close() {} | ||
@@ -326,0 +368,0 @@ } |
@@ -274,3 +274,4 @@ /** | ||
function getGitHubRepoSlug() { | ||
/** @param {string|undefined} apiHost */ | ||
function getGitHubRepoSlug(apiHost = undefined) { | ||
const envSlug = getEnvVarIfSet([ | ||
@@ -297,2 +298,10 @@ // Manual override | ||
} | ||
if (remote && apiHost && !apiHost.includes('github.com')) { | ||
const hostMatch = apiHost.match(/:\/\/(.*?)(\/|$)/); | ||
if (!hostMatch) return undefined; | ||
const remoteRegex = new RegExp(`${hostMatch[1]}(:|\\/)([^/]+\\/.+)\\.git`); | ||
const remoteMatch = remote.match(remoteRegex); | ||
if (remoteMatch) return remoteMatch[2]; | ||
} | ||
} | ||
@@ -299,0 +308,0 @@ |
@@ -145,2 +145,34 @@ /** | ||
/** | ||
* @param {any} o1 | ||
* @param {any} o2 | ||
* @param {number} [depth] | ||
* @return {boolean} | ||
*/ | ||
isEqual(o1, o2, depth = 0) { | ||
// Protect against infinite loops. | ||
if (depth > 100) return false; | ||
// If they don't share the same type they're not equal. | ||
if (typeof o1 !== typeof o2) return false; | ||
// If they're not objects, use referential equality with NaN handling. | ||
if (Number.isNaN(o1) && Number.isNaN(o2)) return true; | ||
if (typeof o1 !== 'object') return o1 === o2; | ||
// Try quick referential equality before perfoming deep comparisons. | ||
if (o1 === o2) return true; | ||
// Check for null. | ||
if (!o1 || !o2) return false; | ||
// At this point we know they're both truthy objects. | ||
// Perform deep inspection on the object's properties. | ||
if (Array.isArray(o1)) { | ||
if (!Array.isArray(o2)) return false; | ||
return o1.every((v, i) => this.isEqual(v, o2[i])) && o1.length === o2.length; | ||
} | ||
const o1Keys = Object.keys(o1).sort(); | ||
const o2Keys = Object.keys(o2).sort(); | ||
const allChildrenEqual = o1Keys.every(k => this.isEqual(o1[k], o2[k], depth + 1)); | ||
return allChildrenEqual && this.isEqual(o1Keys, o2Keys); | ||
}, | ||
/** | ||
* Deep clones an object via JSON.parse/JSON.stringify. | ||
@@ -255,2 +287,7 @@ * @template T | ||
}, | ||
/** @param {string} string @param {number} number @param {string} [pluralForm] */ | ||
pluralize(string, number, pluralForm) { | ||
pluralForm = pluralForm || `${string}s`; | ||
return number === 1 ? string : pluralForm; | ||
}, | ||
uniqueId: (() => { | ||
@@ -257,0 +294,0 @@ let id = 1; |
@@ -197,2 +197,3 @@ /** | ||
token: '', | ||
adminToken: '', | ||
slug: '', | ||
@@ -205,2 +206,3 @@ }, | ||
token: '', | ||
adminToken: '', | ||
slug: '', | ||
@@ -463,2 +465,3 @@ }, | ||
token: '', | ||
adminToken: '', | ||
slug: '', | ||
@@ -465,0 +468,0 @@ }; |
385110
8942