mirror of
https://github.com/tcgdex/javascript-sdk.git
synced 2025-07-12 18:15:10 +00:00
Compare commits
10 Commits
v2.6.0-bet
...
dependabot
Author | SHA1 | Date | |
---|---|---|---|
3f865e1895 | |||
f5dd4cb88c | |||
9b9ff2f028 | |||
ce64a95f4e | |||
460f101026 | |||
b3ba7820a3
|
|||
1f3aae5401 | |||
e501faa823 | |||
e08fd98269 | |||
54a4b729b3 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@ -21,7 +21,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Use Node.js ${{ matrix.node-version }}
|
||||
uses: actions/setup-node@v3
|
||||
|
2
.github/workflows/code-quality.yml
vendored
2
.github/workflows/code-quality.yml
vendored
@ -12,7 +12,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup NodeJS
|
||||
uses: actions/setup-node@v3
|
||||
|
2
.github/workflows/publish.yml
vendored
2
.github/workflows/publish.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Publishing to NPMJS
|
||||
uses: actions/setup-node@v3
|
||||
|
@ -138,3 +138,13 @@ for (const endpoint of endpoints) {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
test(`random card/set/serie`, async () => {
|
||||
const tcgdex = new TCGdex('en')
|
||||
TCGdex.fetch = fetch
|
||||
|
||||
expect((await tcgdex.random.card())).toBeTruthy()
|
||||
expect((await tcgdex.random.set())).toBeTruthy()
|
||||
expect((await tcgdex.random.serie())).toBeTruthy()
|
||||
})
|
||||
|
@ -1,54 +0,0 @@
|
||||
/// <reference types="jest" />
|
||||
|
||||
const { default: MemoryCache } = require("../src/Psr/SimpleCache/MemoryCache")
|
||||
|
||||
const TCGdex = require("../src/tcgdex").default
|
||||
|
||||
test('that cache store and get one element', async () => {
|
||||
const cache = new MemoryCache()
|
||||
cache.set('a', 'b')
|
||||
expect(cache.get('a')).toBe('b')
|
||||
})
|
||||
|
||||
test('that cache store and get multiple elements', async () => {
|
||||
const cache = new MemoryCache()
|
||||
cache.setMultiple({
|
||||
'a': 'b',
|
||||
'c': 'd'
|
||||
})
|
||||
expect(cache.getMultiple(['a', 'c'])).toStrictEqual({
|
||||
a: 'b',
|
||||
c: 'd'
|
||||
})
|
||||
})
|
||||
|
||||
test('cache expiration', async () => {
|
||||
const cache = new MemoryCache()
|
||||
cache.set('a', 'b', 1)
|
||||
// wait 2 secs
|
||||
await new Promise((res) => setTimeout(res, 2000))
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('cache deletion', async () => {
|
||||
const cache = new MemoryCache()
|
||||
cache.set('a', 'b')
|
||||
expect(cache.get('a')).toBe('b')
|
||||
cache.delete('a')
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('cache cleared', async () => {
|
||||
const cache = new MemoryCache()
|
||||
cache.set('a', 'b')
|
||||
expect(cache.get('a')).toBe('b')
|
||||
cache.clear()
|
||||
expect(cache.get('a')).toBeUndefined()
|
||||
})
|
||||
|
||||
test('cache exists', async () => {
|
||||
const cache = new MemoryCache()
|
||||
expect(cache.has('a')).toBe(false)
|
||||
cache.set('a', 'b')
|
||||
expect(cache.has('a')).toBe(true)
|
||||
})
|
349
package-lock.json
generated
349
package-lock.json
generated
@ -1,14 +1,16 @@
|
||||
{
|
||||
"name": "@tcgdex/sdk",
|
||||
"version": "2.6.0-beta.1",
|
||||
"version": "2.6.0-beta.3",
|
||||
"lockfileVersion": 2,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@tcgdex/sdk",
|
||||
"version": "2.6.0-beta.1",
|
||||
"version": "2.6.0-beta.3",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@cachex/memory": "^1.0.0",
|
||||
"@cachex/web-storage": "^1.0.1",
|
||||
"@dzeio/object-util": "^1",
|
||||
"isomorphic-unfetch": "^3"
|
||||
},
|
||||
@ -1723,6 +1725,30 @@
|
||||
"integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@cachex/core": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/core/-/core-1.0.0.tgz",
|
||||
"integrity": "sha512-JZMGqh2mEAB1JqX2A7yMk6FwGMEoGTjw/Hn5UcqWXUq/Etl2cl8Z6xFUraAN9bv7wX3tTwWol/mh5Ej34to8ng==",
|
||||
"dependencies": {
|
||||
"@dzeio/object-util": "^1.8.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@cachex/memory": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/memory/-/memory-1.0.0.tgz",
|
||||
"integrity": "sha512-x/oLaj9ZbwCd4Z+3CA20kPd0mRchNFYt2VMz7vlCczWrhX5Lk1b6Z9xUFWMHpjEb2x1G9K194R+vC5qyj4QslQ==",
|
||||
"dependencies": {
|
||||
"@cachex/core": "^1"
|
||||
}
|
||||
},
|
||||
"node_modules/@cachex/web-storage": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/web-storage/-/web-storage-1.0.1.tgz",
|
||||
"integrity": "sha512-E8Xa9qDZgNgr+lcj3eixowg7PH2CVZbp3huuoc5xVVTtwYrZi5YqbHBG12yG3r6C6Fts/2Yoq6cbVBSm6c8VRA==",
|
||||
"dependencies": {
|
||||
"@cachex/core": "^1"
|
||||
}
|
||||
},
|
||||
"node_modules/@dzeio/config": {
|
||||
"version": "1.1.12",
|
||||
"resolved": "https://registry.npmjs.org/@dzeio/config/-/config-1.1.12.tgz",
|
||||
@ -1755,9 +1781,9 @@
|
||||
"integrity": "sha512-/d0ezut7EGrEKedcD8K2Jb2NAMSFfhxNj4rpUBlGzmmakJjJCXAgXvSDLjUwYrgHuabxbxlAn90Wo727MCzWLA=="
|
||||
},
|
||||
"node_modules/@eslint/eslintrc": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.4.1.tgz",
|
||||
"integrity": "sha512-XXrH9Uarn0stsyldqDYq8r++mROmWRI1xKMXa640Bb//SY1+ECYX6VzT6Lcx5frD0V30XieqJ0oX9I2Xj5aoMA==",
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-2.0.0.tgz",
|
||||
"integrity": "sha512-fluIaaV+GyV24CCu/ggiHdV+j4RNh85yQnAYS/G2mZODZgGmmlrgCydjUcV3YvxCm9x8nMAfThsqTni4KiXT4A==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"ajv": "^6.12.4",
|
||||
@ -1778,9 +1804,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@eslint/eslintrc/node_modules/globals": {
|
||||
"version": "13.19.0",
|
||||
"resolved": "https://registry.npmjs.org/globals/-/globals-13.19.0.tgz",
|
||||
"integrity": "sha512-dkQ957uSRWHw7CFXLUtUHQI3g3aWApYhfNR2O6jn/907riyTYKVBmxYVROkBcY614FSSeSJh7Xm7SrUWCxvJMQ==",
|
||||
"version": "13.20.0",
|
||||
"resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz",
|
||||
"integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"type-fest": "^0.20.2"
|
||||
@ -1804,6 +1830,15 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"node_modules/@eslint/js": {
|
||||
"version": "8.35.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.35.0.tgz",
|
||||
"integrity": "sha512-JXdzbRiWclLVoD8sNUjR443VVlYqiYmDVT6rGUEIEHU5YJW0gaVZwV2xgM7D4arkvASqD0IlLUVjHiFuxaftRw==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@humanwhocodes/config-array": {
|
||||
"version": "0.11.8",
|
||||
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz",
|
||||
@ -2884,14 +2919,14 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser": {
|
||||
"version": "5.48.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.48.1.tgz",
|
||||
"integrity": "sha512-4yg+FJR/V1M9Xoq56SF9Iygqm+r5LMXvheo6DQ7/yUWynQ4YfCRnsKuRgqH4EQ5Ya76rVwlEpw4Xu+TgWQUcdA==",
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.54.1.tgz",
|
||||
"integrity": "sha512-8zaIXJp/nG9Ff9vQNh7TI+C3nA6q6iIsGJ4B4L6MhZ7mHnTMR4YP5vp2xydmFXIy8rpyIVbNAG44871LMt6ujg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@typescript-eslint/scope-manager": "5.48.1",
|
||||
"@typescript-eslint/types": "5.48.1",
|
||||
"@typescript-eslint/typescript-estree": "5.48.1",
|
||||
"@typescript-eslint/scope-manager": "5.54.1",
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/typescript-estree": "5.54.1",
|
||||
"debug": "^4.3.4"
|
||||
},
|
||||
"engines": {
|
||||
@ -2910,6 +2945,113 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/scope-manager": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.54.1.tgz",
|
||||
"integrity": "sha512-zWKuGliXxvuxyM71UA/EcPxaviw39dB2504LqAmFDjmkpO8qNLHcmzlh6pbHs1h/7YQ9bnsO8CCcYCSA8sykUg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/visitor-keys": "5.54.1"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/typescript-eslint"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/types": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.54.1.tgz",
|
||||
"integrity": "sha512-G9+1vVazrfAfbtmCapJX8jRo2E4MDXxgm/IMOF4oGh3kq7XuK3JRkOg6y2Qu1VsTRmWETyTkWt1wxy7X7/yLkw==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/typescript-eslint"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/typescript-estree": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.54.1.tgz",
|
||||
"integrity": "sha512-bjK5t+S6ffHnVwA0qRPTZrxKSaFYocwFIkZx5k7pvWfsB1I57pO/0M0Skatzzw1sCkjJ83AfGTL0oFIFiDX3bg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/visitor-keys": "5.54.1",
|
||||
"debug": "^4.3.4",
|
||||
"globby": "^11.1.0",
|
||||
"is-glob": "^4.0.3",
|
||||
"semver": "^7.3.7",
|
||||
"tsutils": "^3.21.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/typescript-eslint"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"typescript": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/@typescript-eslint/visitor-keys": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.54.1.tgz",
|
||||
"integrity": "sha512-q8iSoHTgwCfgcRJ2l2x+xCbu8nBlRAlsQ33k24Adj8eoVBE0f8dUeI+bAa8F84Mv05UGbAx57g2zrRsYIooqQg==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"eslint-visitor-keys": "^3.3.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
|
||||
},
|
||||
"funding": {
|
||||
"type": "opencollective",
|
||||
"url": "https://opencollective.com/typescript-eslint"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/lru-cache": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
|
||||
"integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"yallist": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/semver": {
|
||||
"version": "7.3.8",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz",
|
||||
"integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"lru-cache": "^6.0.0"
|
||||
},
|
||||
"bin": {
|
||||
"semver": "bin/semver.js"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=10"
|
||||
}
|
||||
},
|
||||
"node_modules/@typescript-eslint/parser/node_modules/yallist": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
|
||||
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/@typescript-eslint/scope-manager": {
|
||||
"version": "5.48.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.48.1.tgz",
|
||||
@ -3113,9 +3255,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/acorn": {
|
||||
"version": "8.8.1",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.1.tgz",
|
||||
"integrity": "sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==",
|
||||
"version": "8.8.2",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz",
|
||||
"integrity": "sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
@ -3943,12 +4085,13 @@
|
||||
}
|
||||
},
|
||||
"node_modules/eslint": {
|
||||
"version": "8.31.0",
|
||||
"resolved": "https://registry.npmjs.org/eslint/-/eslint-8.31.0.tgz",
|
||||
"integrity": "sha512-0tQQEVdmPZ1UtUKXjX7EMm9BlgJ08G90IhWh0PKDCb3ZLsgAOHI8fYSIzYVZej92zsgq+ft0FGsxhJ3xo2tbuA==",
|
||||
"version": "8.35.0",
|
||||
"resolved": "https://registry.npmjs.org/eslint/-/eslint-8.35.0.tgz",
|
||||
"integrity": "sha512-BxAf1fVL7w+JLRQhWl2pzGeSiGqbWumV4WNvc9Rhp6tiCtm4oHnyPBSEtMGZwrQgudFQ+otqzWoPB7x+hxoWsw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@eslint/eslintrc": "^1.4.1",
|
||||
"@eslint/eslintrc": "^2.0.0",
|
||||
"@eslint/js": "8.35.0",
|
||||
"@humanwhocodes/config-array": "^0.11.8",
|
||||
"@humanwhocodes/module-importer": "^1.0.1",
|
||||
"@nodelib/fs.walk": "^1.2.8",
|
||||
@ -3962,7 +4105,7 @@
|
||||
"eslint-utils": "^3.0.0",
|
||||
"eslint-visitor-keys": "^3.3.0",
|
||||
"espree": "^9.4.0",
|
||||
"esquery": "^1.4.0",
|
||||
"esquery": "^1.4.2",
|
||||
"esutils": "^2.0.2",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"file-entry-cache": "^6.0.1",
|
||||
@ -4209,9 +4352,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/esquery": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.4.0.tgz",
|
||||
"integrity": "sha512-cCDispWt5vHHtwMY2YrAQ4ibFkAL8RbH5YGBnZBc90MolvvfkkQcJro/aZiAQUlQ3qgrYS6D6v8Gc5G5CQsc9w==",
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.4.2.tgz",
|
||||
"integrity": "sha512-JVSoLdTlTDkmjFmab7H/9SL9qGSyjElT3myyKp7krqjVFQCDLmj1QFaCLRFBszBKI0XVZaiiXvuPIX3ZwHe1Ng==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"estraverse": "^5.1.0"
|
||||
@ -7546,9 +7689,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"node_modules/punycode": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.2.0.tgz",
|
||||
"integrity": "sha512-LN6QV1IJ9ZhxWTNdktaPClrNfp8xdSAYS0Zk2ddX7XsXZAxckMHPCBcHRo0cTcEIgYPRiGEkmji3Idkh2yFtYw==",
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz",
|
||||
"integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
|
||||
"dev": true,
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
@ -10384,6 +10527,30 @@
|
||||
"integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==",
|
||||
"dev": true
|
||||
},
|
||||
"@cachex/core": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/core/-/core-1.0.0.tgz",
|
||||
"integrity": "sha512-JZMGqh2mEAB1JqX2A7yMk6FwGMEoGTjw/Hn5UcqWXUq/Etl2cl8Z6xFUraAN9bv7wX3tTwWol/mh5Ej34to8ng==",
|
||||
"requires": {
|
||||
"@dzeio/object-util": "^1.8.3"
|
||||
}
|
||||
},
|
||||
"@cachex/memory": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/memory/-/memory-1.0.0.tgz",
|
||||
"integrity": "sha512-x/oLaj9ZbwCd4Z+3CA20kPd0mRchNFYt2VMz7vlCczWrhX5Lk1b6Z9xUFWMHpjEb2x1G9K194R+vC5qyj4QslQ==",
|
||||
"requires": {
|
||||
"@cachex/core": "^1"
|
||||
}
|
||||
},
|
||||
"@cachex/web-storage": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/@cachex/web-storage/-/web-storage-1.0.1.tgz",
|
||||
"integrity": "sha512-E8Xa9qDZgNgr+lcj3eixowg7PH2CVZbp3huuoc5xVVTtwYrZi5YqbHBG12yG3r6C6Fts/2Yoq6cbVBSm6c8VRA==",
|
||||
"requires": {
|
||||
"@cachex/core": "^1"
|
||||
}
|
||||
},
|
||||
"@dzeio/config": {
|
||||
"version": "1.1.12",
|
||||
"resolved": "https://registry.npmjs.org/@dzeio/config/-/config-1.1.12.tgz",
|
||||
@ -10399,9 +10566,9 @@
|
||||
"integrity": "sha512-/d0ezut7EGrEKedcD8K2Jb2NAMSFfhxNj4rpUBlGzmmakJjJCXAgXvSDLjUwYrgHuabxbxlAn90Wo727MCzWLA=="
|
||||
},
|
||||
"@eslint/eslintrc": {
|
||||
"version": "1.4.1",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-1.4.1.tgz",
|
||||
"integrity": "sha512-XXrH9Uarn0stsyldqDYq8r++mROmWRI1xKMXa640Bb//SY1+ECYX6VzT6Lcx5frD0V30XieqJ0oX9I2Xj5aoMA==",
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/eslintrc/-/eslintrc-2.0.0.tgz",
|
||||
"integrity": "sha512-fluIaaV+GyV24CCu/ggiHdV+j4RNh85yQnAYS/G2mZODZgGmmlrgCydjUcV3YvxCm9x8nMAfThsqTni4KiXT4A==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"ajv": "^6.12.4",
|
||||
@ -10416,9 +10583,9 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"globals": {
|
||||
"version": "13.19.0",
|
||||
"resolved": "https://registry.npmjs.org/globals/-/globals-13.19.0.tgz",
|
||||
"integrity": "sha512-dkQ957uSRWHw7CFXLUtUHQI3g3aWApYhfNR2O6jn/907riyTYKVBmxYVROkBcY614FSSeSJh7Xm7SrUWCxvJMQ==",
|
||||
"version": "13.20.0",
|
||||
"resolved": "https://registry.npmjs.org/globals/-/globals-13.20.0.tgz",
|
||||
"integrity": "sha512-Qg5QtVkCy/kv3FUSlu4ukeZDVf9ee0iXLAUYX13gbR17bnejFTzr4iS9bY7kwCf1NztRNm1t91fjOiyx4CSwPQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"type-fest": "^0.20.2"
|
||||
@ -10432,6 +10599,12 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"@eslint/js": {
|
||||
"version": "8.35.0",
|
||||
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.35.0.tgz",
|
||||
"integrity": "sha512-JXdzbRiWclLVoD8sNUjR443VVlYqiYmDVT6rGUEIEHU5YJW0gaVZwV2xgM7D4arkvASqD0IlLUVjHiFuxaftRw==",
|
||||
"dev": true
|
||||
},
|
||||
"@humanwhocodes/config-array": {
|
||||
"version": "0.11.8",
|
||||
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.8.tgz",
|
||||
@ -11283,15 +11456,82 @@
|
||||
}
|
||||
},
|
||||
"@typescript-eslint/parser": {
|
||||
"version": "5.48.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.48.1.tgz",
|
||||
"integrity": "sha512-4yg+FJR/V1M9Xoq56SF9Iygqm+r5LMXvheo6DQ7/yUWynQ4YfCRnsKuRgqH4EQ5Ya76rVwlEpw4Xu+TgWQUcdA==",
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-5.54.1.tgz",
|
||||
"integrity": "sha512-8zaIXJp/nG9Ff9vQNh7TI+C3nA6q6iIsGJ4B4L6MhZ7mHnTMR4YP5vp2xydmFXIy8rpyIVbNAG44871LMt6ujg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@typescript-eslint/scope-manager": "5.48.1",
|
||||
"@typescript-eslint/types": "5.48.1",
|
||||
"@typescript-eslint/typescript-estree": "5.48.1",
|
||||
"@typescript-eslint/scope-manager": "5.54.1",
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/typescript-estree": "5.54.1",
|
||||
"debug": "^4.3.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"@typescript-eslint/scope-manager": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-5.54.1.tgz",
|
||||
"integrity": "sha512-zWKuGliXxvuxyM71UA/EcPxaviw39dB2504LqAmFDjmkpO8qNLHcmzlh6pbHs1h/7YQ9bnsO8CCcYCSA8sykUg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/visitor-keys": "5.54.1"
|
||||
}
|
||||
},
|
||||
"@typescript-eslint/types": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-5.54.1.tgz",
|
||||
"integrity": "sha512-G9+1vVazrfAfbtmCapJX8jRo2E4MDXxgm/IMOF4oGh3kq7XuK3JRkOg6y2Qu1VsTRmWETyTkWt1wxy7X7/yLkw==",
|
||||
"dev": true
|
||||
},
|
||||
"@typescript-eslint/typescript-estree": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-5.54.1.tgz",
|
||||
"integrity": "sha512-bjK5t+S6ffHnVwA0qRPTZrxKSaFYocwFIkZx5k7pvWfsB1I57pO/0M0Skatzzw1sCkjJ83AfGTL0oFIFiDX3bg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"@typescript-eslint/visitor-keys": "5.54.1",
|
||||
"debug": "^4.3.4",
|
||||
"globby": "^11.1.0",
|
||||
"is-glob": "^4.0.3",
|
||||
"semver": "^7.3.7",
|
||||
"tsutils": "^3.21.0"
|
||||
}
|
||||
},
|
||||
"@typescript-eslint/visitor-keys": {
|
||||
"version": "5.54.1",
|
||||
"resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-5.54.1.tgz",
|
||||
"integrity": "sha512-q8iSoHTgwCfgcRJ2l2x+xCbu8nBlRAlsQ33k24Adj8eoVBE0f8dUeI+bAa8F84Mv05UGbAx57g2zrRsYIooqQg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@typescript-eslint/types": "5.54.1",
|
||||
"eslint-visitor-keys": "^3.3.0"
|
||||
}
|
||||
},
|
||||
"lru-cache": {
|
||||
"version": "6.0.0",
|
||||
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz",
|
||||
"integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"yallist": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"semver": {
|
||||
"version": "7.3.8",
|
||||
"resolved": "https://registry.npmjs.org/semver/-/semver-7.3.8.tgz",
|
||||
"integrity": "sha512-NB1ctGL5rlHrPJtFDVIVzTyQylMLu9N9VICA6HSFJo8MCGVTMW6gfpicwKmmK/dAjTOrqu5l63JJOpDSrAis3A==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"lru-cache": "^6.0.0"
|
||||
}
|
||||
},
|
||||
"yallist": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
|
||||
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"@typescript-eslint/scope-manager": {
|
||||
@ -11425,9 +11665,9 @@
|
||||
}
|
||||
},
|
||||
"acorn": {
|
||||
"version": "8.8.1",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.1.tgz",
|
||||
"integrity": "sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==",
|
||||
"version": "8.8.2",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.8.2.tgz",
|
||||
"integrity": "sha512-xjIYgE8HBrkpd/sJqOGNspf8uHG+NOHGOw6a/Urj8taM2EXfdNAH2oFcPeIFfsv3+kz/mJrS5VuMqbNLjCa2vw==",
|
||||
"dev": true
|
||||
},
|
||||
"acorn-jsx": {
|
||||
@ -12038,12 +12278,13 @@
|
||||
"dev": true
|
||||
},
|
||||
"eslint": {
|
||||
"version": "8.31.0",
|
||||
"resolved": "https://registry.npmjs.org/eslint/-/eslint-8.31.0.tgz",
|
||||
"integrity": "sha512-0tQQEVdmPZ1UtUKXjX7EMm9BlgJ08G90IhWh0PKDCb3ZLsgAOHI8fYSIzYVZej92zsgq+ft0FGsxhJ3xo2tbuA==",
|
||||
"version": "8.35.0",
|
||||
"resolved": "https://registry.npmjs.org/eslint/-/eslint-8.35.0.tgz",
|
||||
"integrity": "sha512-BxAf1fVL7w+JLRQhWl2pzGeSiGqbWumV4WNvc9Rhp6tiCtm4oHnyPBSEtMGZwrQgudFQ+otqzWoPB7x+hxoWsw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@eslint/eslintrc": "^1.4.1",
|
||||
"@eslint/eslintrc": "^2.0.0",
|
||||
"@eslint/js": "8.35.0",
|
||||
"@humanwhocodes/config-array": "^0.11.8",
|
||||
"@humanwhocodes/module-importer": "^1.0.1",
|
||||
"@nodelib/fs.walk": "^1.2.8",
|
||||
@ -12057,7 +12298,7 @@
|
||||
"eslint-utils": "^3.0.0",
|
||||
"eslint-visitor-keys": "^3.3.0",
|
||||
"espree": "^9.4.0",
|
||||
"esquery": "^1.4.0",
|
||||
"esquery": "^1.4.2",
|
||||
"esutils": "^2.0.2",
|
||||
"fast-deep-equal": "^3.1.3",
|
||||
"file-entry-cache": "^6.0.1",
|
||||
@ -12223,9 +12464,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"esquery": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.4.0.tgz",
|
||||
"integrity": "sha512-cCDispWt5vHHtwMY2YrAQ4ibFkAL8RbH5YGBnZBc90MolvvfkkQcJro/aZiAQUlQ3qgrYS6D6v8Gc5G5CQsc9w==",
|
||||
"version": "1.4.2",
|
||||
"resolved": "https://registry.npmjs.org/esquery/-/esquery-1.4.2.tgz",
|
||||
"integrity": "sha512-JVSoLdTlTDkmjFmab7H/9SL9qGSyjElT3myyKp7krqjVFQCDLmj1QFaCLRFBszBKI0XVZaiiXvuPIX3ZwHe1Ng==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"estraverse": "^5.1.0"
|
||||
@ -14754,9 +14995,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"punycode": {
|
||||
"version": "2.2.0",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.2.0.tgz",
|
||||
"integrity": "sha512-LN6QV1IJ9ZhxWTNdktaPClrNfp8xdSAYS0Zk2ddX7XsXZAxckMHPCBcHRo0cTcEIgYPRiGEkmji3Idkh2yFtYw==",
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.0.tgz",
|
||||
"integrity": "sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==",
|
||||
"dev": true
|
||||
},
|
||||
"queue-microtask": {
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@tcgdex/sdk",
|
||||
"version": "2.6.0-beta.2",
|
||||
"version": "2.6.0-beta.4",
|
||||
"main": "./dist/tcgdex.node.js",
|
||||
"module": "./dist/tcgdex.node.mjs",
|
||||
"types": "./dist/tcgdex.node.d.ts",
|
||||
@ -53,12 +53,14 @@
|
||||
"node": ">=12"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cachex/memory": "^1",
|
||||
"@cachex/web-storage": "^1",
|
||||
"@dzeio/object-util": "^1",
|
||||
"isomorphic-unfetch": "^3"
|
||||
},
|
||||
"scripts": {
|
||||
"prebuild": "node scripts/export-version-number.js",
|
||||
"build": "rm -r dist && tsup ./src/tcgdex.node.ts --format cjs,esm --dts --clean && tsup ./src/tcgdex.browser.ts --format iife --global-name TCGdex --sourcemap",
|
||||
"build": "rm -rf dist && tsup ./src/tcgdex.node.ts --format cjs,esm --dts --clean && tsup ./src/tcgdex.browser.ts --format iife --global-name TCGdex --sourcemap",
|
||||
"prepublishOnly": "npm run build",
|
||||
"lint": "eslint",
|
||||
"test": "jest --coverage"
|
||||
|
@ -1,38 +0,0 @@
|
||||
import { objectLoop } from '@dzeio/object-util'
|
||||
import type CacheInterface from './CacheInterface'
|
||||
|
||||
export default abstract class CacheAsbract implements CacheInterface {
|
||||
|
||||
public getMultiple<T>(keys: Array<string>, defaultValues?: Array<T> | undefined): Record<string, T> {
|
||||
const res: Record<string, T> = {}
|
||||
for (let idx = 0; idx < keys.length; idx++) {
|
||||
const key = keys[idx] as string
|
||||
const value = this.get(key, defaultValues?.[idx]) as T | undefined
|
||||
if (typeof value === 'undefined') {
|
||||
continue
|
||||
}
|
||||
res[key] = value
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
public setMultiple<T>(values: Record<string, T>, ttl?: number | undefined): boolean {
|
||||
objectLoop(values, (v, k) => {
|
||||
this.set(k, v, ttl)
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
public deleteMultiple(keys: Array<string>): boolean {
|
||||
for (const key of keys) {
|
||||
this.delete(key)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public abstract get<T>(key: string, defaultValue?: T): T | undefined
|
||||
public abstract set<T>(key: string, value: T, ttl?: number): boolean
|
||||
public abstract delete(key: string): boolean
|
||||
public abstract clear(): boolean
|
||||
public abstract has(key: string): boolean
|
||||
}
|
109
src/Psr/SimpleCache/CacheInterface.d.ts
vendored
109
src/Psr/SimpleCache/CacheInterface.d.ts
vendored
@ -1,109 +0,0 @@
|
||||
export default interface CacheInterface {
|
||||
/**
|
||||
* Fetches a value from the cache.
|
||||
*
|
||||
* @param key The unique key of this item in the cache.
|
||||
* @param defaultValue Default value to return if the key does not exist.
|
||||
*
|
||||
* @return T The value of the item from the cache, or $default in case of cache miss.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if the $key string is not a legal value.
|
||||
*/
|
||||
get<T>(key: string, defaultValue?: T): T | undefined
|
||||
|
||||
/**
|
||||
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
|
||||
*
|
||||
* @param key The key of the item to store.
|
||||
* @param value The value of the item to store. Must be serializable.
|
||||
* @param {null|number} ttl The TTL value of this item. If no value is sent and
|
||||
* the driver supports TTL then the library may set a default value
|
||||
* for it or let the driver take care of that.
|
||||
*
|
||||
* @return bool True on success and false on failure.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if the $key string is not a legal value.
|
||||
*/
|
||||
set<T>(key: string, value: T, ttl?: number): boolean
|
||||
|
||||
/**
|
||||
* Delete an item from the cache by its unique key.
|
||||
*
|
||||
* @param key The unique cache key of the item to delete.
|
||||
*
|
||||
* @return True if the item was successfully removed. False if there was an error.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if the $key string is not a legal value.
|
||||
*/
|
||||
delete(key: string): boolean
|
||||
|
||||
/**
|
||||
* Wipes clean the entire cache's keys.
|
||||
*
|
||||
* @return boolean True on success and false on failure.
|
||||
*/
|
||||
clear(): boolean
|
||||
|
||||
/**
|
||||
* Obtains multiple cache items by their unique keys.
|
||||
*
|
||||
* @param keys A list of keys that can obtained in a single operation.
|
||||
* @param defaultValues $default Default value to return for keys that do not exist.
|
||||
*
|
||||
* @return iterable A list of key => value pairs. Cache keys that do not exist or are stale will have $default as value.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if $keys is neither an array nor a Traversable,
|
||||
* or if any of the $keys are not a legal value.
|
||||
*/
|
||||
getMultiple<T>(keys: Array<string>, defaultValues?: Array<T>): Record<string, T>
|
||||
|
||||
/**
|
||||
* Persists a set of key => value pairs in the cache, with an optional TTL.
|
||||
*
|
||||
* @param values A list of key => value pairs for a multiple-set operation.
|
||||
* @param ttl Optional. The TTL value of this item. If no value is sent and
|
||||
* the driver supports TTL then the library may set a default value
|
||||
* for it or let the driver take care of that.
|
||||
*
|
||||
* @return bool True on success and false on failure.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if $values is neither an array nor a Traversable,
|
||||
* or if any of the $values are not a legal value.
|
||||
*/
|
||||
setMultiple<T>(values: Record<string, T>, ttl?: number): boolean
|
||||
|
||||
/**
|
||||
* Deletes multiple cache items in a single operation.
|
||||
*
|
||||
* @param keys A list of string-based keys to be deleted.
|
||||
*
|
||||
* @return bool True if the items were successfully removed. False if there was an error.
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if $keys is neither an array nor a Traversable,
|
||||
* or if any of the $keys are not a legal value.
|
||||
*/
|
||||
deleteMultiple(keys: Array<string>): boolean
|
||||
|
||||
/**
|
||||
* Determines whether an item is present in the cache.
|
||||
*
|
||||
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
|
||||
* and not to be used within your live applications operations for get/set, as this method
|
||||
* is subject to a race condition where your has() will return true and immediately after,
|
||||
* another script can remove it, making the state of your app out of date.
|
||||
*
|
||||
* @param key The cache item key.
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
* MUST be thrown if the $key string is not a legal value.
|
||||
*/
|
||||
has(key: string): boolean
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
import CacheAsbract from './CacheAbstract'
|
||||
|
||||
interface CacheItem<T> {
|
||||
data: T
|
||||
expire?: number | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* A cache implementation that uses browser storage.
|
||||
*
|
||||
* This class extends `CacheAsbract` and provides a concrete implementation
|
||||
* of the caching interface. It stores cached items in browser storage,
|
||||
* which is suitable for storing small amounts of data.
|
||||
*/
|
||||
export default class BrowserStorageCache extends CacheAsbract {
|
||||
private storage: Storage
|
||||
public constructor(private readonly prefix?: string, session = false) {
|
||||
super()
|
||||
if (session) {
|
||||
this.storage = window.sessionStorage
|
||||
} else {
|
||||
this.storage = window.localStorage
|
||||
}
|
||||
}
|
||||
|
||||
public get<T>(key: string, defaultValue?: T | undefined): T | undefined {
|
||||
const raw = this.storage.getItem(this.getFinalKey(key))
|
||||
|
||||
if (!raw) {
|
||||
return defaultValue ?? undefined
|
||||
}
|
||||
|
||||
const item: CacheItem<T> = JSON.parse(raw)
|
||||
|
||||
if (item.expire && item.expire < new Date().getTime()) {
|
||||
this.delete(key)
|
||||
return defaultValue ?? undefined
|
||||
}
|
||||
|
||||
return item.data
|
||||
}
|
||||
|
||||
public set<T>(key: string, value: T, ttl?: number | undefined): boolean {
|
||||
let expire = undefined
|
||||
if (ttl) {
|
||||
expire = (new Date()).getTime() + (ttl * 1000)
|
||||
}
|
||||
const data: CacheItem<unknown> = {
|
||||
data: value,
|
||||
expire: expire
|
||||
}
|
||||
this.storage.setItem(this.getFinalKey(key), JSON.stringify(data))
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
public delete(key: string): boolean {
|
||||
this.storage.removeItem(this.getFinalKey(key))
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
public clear(): boolean {
|
||||
const keys = this.keys()
|
||||
return this.deleteMultiple(keys)
|
||||
}
|
||||
|
||||
public has(key: string): boolean {
|
||||
return !!this.storage.getItem(this.getFinalKey(key))
|
||||
}
|
||||
|
||||
private keys(): Array<string> {
|
||||
const list: Array<string> = []
|
||||
for (let idx = 0; idx < this.storage.length; idx++) {
|
||||
const key = this.storage.key(idx)
|
||||
if (!key || this.prefix && !key?.startsWith(`${this.prefix}/`)) {
|
||||
continue
|
||||
}
|
||||
list.push(key)
|
||||
}
|
||||
|
||||
return list
|
||||
}
|
||||
|
||||
private getFinalKey(key: string): string {
|
||||
if (!this.prefix) {
|
||||
return key
|
||||
}
|
||||
return `${this.prefix}/${key}`
|
||||
}
|
||||
}
|
@ -1,58 +0,0 @@
|
||||
import CacheAsbract from './CacheAbstract'
|
||||
|
||||
interface CacheItem<T> {
|
||||
data: T
|
||||
expire?: number | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Memory cache implementation that stores cached items in memory.
|
||||
* This class extends the abstract `CacheAbstract` and provides a basic in-memory caching mechanism.
|
||||
*
|
||||
* @class MemoryCache
|
||||
*/
|
||||
export default class MemoryCache extends CacheAsbract {
|
||||
private cache: Map<string, CacheItem<unknown>> = new Map()
|
||||
|
||||
public get<T>(key: string, defaultValue?: T | undefined): T | undefined {
|
||||
const item = this.cache.get(key)
|
||||
|
||||
if (!item) {
|
||||
return defaultValue ?? undefined
|
||||
}
|
||||
|
||||
if (item.expire && item.expire < new Date().getTime()) {
|
||||
this.delete(key)
|
||||
return defaultValue ?? undefined
|
||||
}
|
||||
|
||||
return item.data as T | undefined
|
||||
}
|
||||
|
||||
public set<T>(key: string, value: T, ttl?: number | undefined): boolean {
|
||||
let expire: number | undefined
|
||||
if (ttl) {
|
||||
expire = new Date().getTime() + ttl * 1000
|
||||
}
|
||||
this.cache.set(key, {
|
||||
data: value,
|
||||
expire: expire
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
public delete(key: string): boolean {
|
||||
this.cache.delete(key)
|
||||
return true
|
||||
}
|
||||
|
||||
public clear(): boolean {
|
||||
this.cache.clear()
|
||||
return true
|
||||
}
|
||||
|
||||
public has(key: string): boolean {
|
||||
return this.cache.has(key)
|
||||
}
|
||||
}
|
6
src/interfaces.d.ts
vendored
6
src/interfaces.d.ts
vendored
@ -316,6 +316,6 @@ export type Quality = 'low' | 'high'
|
||||
export type Extension = 'jpg' | 'webp' | 'png'
|
||||
|
||||
export type Endpoints = 'cards' | 'categories' | 'dex-ids' | 'energy-types' |
|
||||
'hp' | 'illustrators' | 'rarities' | 'regulation-marks' |
|
||||
'retreats' | 'series' | 'sets' | 'stages' | 'suffixes' |
|
||||
'trainer-types' | 'types' | 'variants'
|
||||
'hp' | 'illustrators' | 'rarities' | 'regulation-marks' |
|
||||
'retreats' | 'series' | 'sets' | 'stages' | 'suffixes' |
|
||||
'trainer-types' | 'types' | 'variants' | 'random'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import type CacheInterface from './Psr/SimpleCache/CacheInterface'
|
||||
import LocalStorageCache from './Psr/SimpleCache/LocalStorageCache'
|
||||
import MemoryCache from './Psr/SimpleCache/MemoryCache'
|
||||
import type CacheInterface from '@cachex/core'
|
||||
import LocalStorageCache from '@cachex/web-storage'
|
||||
import MemoryCache from '@cachex/memory'
|
||||
import Query from './Query'
|
||||
import Endpoint from './endpoints/Endpoint'
|
||||
import SimpleEndpoint from './endpoints/SimpleEndpoint'
|
||||
@ -17,6 +17,7 @@ import type {
|
||||
} from './interfaces'
|
||||
import CardModel from './models/Card'
|
||||
import CardResumeModel from './models/CardResume'
|
||||
import Model from './models/Model'
|
||||
import SerieModel from './models/Serie'
|
||||
import SerieResume from './models/SerieResume'
|
||||
import SetModel from './models/Set'
|
||||
@ -48,6 +49,22 @@ export default class TCGdex {
|
||||
*/
|
||||
public cacheTTL = 60 * 60
|
||||
|
||||
// random card/set/serie endpoints
|
||||
public readonly random = {
|
||||
card: async (): Promise<CardModel> => {
|
||||
const res = await this.fetch('random', 'card')
|
||||
return Model.build(new CardModel(this), res)
|
||||
},
|
||||
set: async (): Promise<SetModel> => {
|
||||
const res = await this.fetch('random', 'set')
|
||||
return Model.build(new SetModel(this), res)
|
||||
},
|
||||
serie: async (): Promise<SerieModel> => {
|
||||
const res = await this.fetch('random', 'serie')
|
||||
return Model.build(new SerieModel(this), res)
|
||||
}
|
||||
}
|
||||
|
||||
public readonly card = new Endpoint(this, CardModel, CardResumeModel, 'cards')
|
||||
public readonly set = new Endpoint(this, SetModel, SetResumeModel, 'sets')
|
||||
public readonly serie = new Endpoint(this, SerieModel, SerieResume, 'series')
|
||||
@ -226,6 +243,13 @@ export default class TCGdex {
|
||||
*/
|
||||
public async fetch(...endpoint: ['sets', string]): Promise<TCGdexSet | undefined>
|
||||
|
||||
/**
|
||||
* Fetch a random element
|
||||
* @param endpoint_0 'random'
|
||||
* @param endpoint_1 {'set' | 'card' | 'serie'} the type of random element you want to get
|
||||
*/
|
||||
public async fetch(...endpoint: ['random', 'set' | 'card' | 'serie']): Promise<Card | TCGdexSet | Serie | undefined>
|
||||
|
||||
/**
|
||||
* Fetch every sets
|
||||
* @param endpoint_0 'sets'
|
||||
@ -310,14 +334,7 @@ export default class TCGdex {
|
||||
|
||||
// handle the Search Params
|
||||
if (searchParams) {
|
||||
path += '?'
|
||||
for (let idx = 0; idx < searchParams.length; idx++) {
|
||||
const param = searchParams[idx]
|
||||
if (idx !== 0) {
|
||||
path += '&'
|
||||
}
|
||||
path += `${this.encode(param.key)}=${this.encode(param.value)}`
|
||||
}
|
||||
path += '?' + searchParams.map((it) => `${this.encode(it.key)}=${this.encode(it.value)}`).join('&')
|
||||
}
|
||||
|
||||
// return with the endpoint and all the shit
|
||||
@ -385,4 +402,3 @@ export * from './models/Card'
|
||||
export {
|
||||
Query
|
||||
}
|
||||
|
||||
|
@ -16,5 +16,5 @@ export const ENDPOINTS: Array<Endpoints> = [
|
||||
'cards', 'categories', 'dex-ids', 'energy-types',
|
||||
'hp', 'illustrators', 'rarities', 'regulation-marks',
|
||||
'retreats', 'series', 'sets', 'stages', 'suffixes',
|
||||
'trainer-types', 'types', 'variants'
|
||||
'trainer-types', 'types', 'variants', 'random'
|
||||
] as const
|
||||
|
Reference in New Issue
Block a user