Skip to content

Commit

Permalink
refactor: improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TIMMLOPK committed Aug 9, 2024
1 parent 16292e1 commit 1bd866f
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 14 deletions.
1 change: 1 addition & 0 deletions apps/website/pages/api_reference/routes/base.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Represents any API route that can be used to fetch data.
| Option | Type | Description |
| --------- | --------- | ------------------------------ |
| `maxSize` | `number?` | The maximum size of the cache. |
| `maxAge` | `number?` | The maximum age of the cache. |
5 changes: 3 additions & 2 deletions apps/website/pages/cache.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

genshin-kit.js has cached most of responses from the API(except some routes you can check **API Reference**), so you can use cache instead making a new request.

**Note**: The key of cache is the UID, cookie or other things you use to make the request.

**Note**: In most of the cases, the key of cache is the `UID`.
```javascript
//...
<ROUTER>.cache.get("key");
Expand All @@ -16,6 +15,8 @@ You can customize it by passing `cacheOptions` to the constructor of router.
```javascript
//...
cacheOptions: {
maxAge: 60, // the cache will be deleted after 60 seconds
maxSize: 0, // it wouldn't cache anythings
},
//...
```
2 changes: 1 addition & 1 deletion apps/website/pages/daily-rewards-checkin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ await client.dailyReward.checkIn({
```

When you solve the geetest, Genshin-kit.js will send new request to the server with the `validate` and `seccode` you returned.
Or else, it will return status `geetest triggered`.
Otherwise, it will return status `geetest triggered`.
14 changes: 10 additions & 4 deletions apps/website/pages/getting-started.mdx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Tab, Tabs } from "nextra-theme-docs";
import { Steps } from 'nextra/components'

# Getting Started

## Installation
<Steps>
### Installation

**Node.js 16.9.0 or newer is required.**

Expand All @@ -24,11 +26,10 @@ import { Tab, Tabs } from "nextra-theme-docs";
</Tab>
</Tabs>

## Quick Start

### Getting Started
```js copy
const { Client, Language, SpiralAbyss } = require("genshin-kit.js");
// import { Client, Language, SpiralAbyss } from "genshin-kit.js"; (if you use typescript)
// import { Client, Language, SpiralAbyss } from "genshin-kit.js"; (if you use typescript/ESM)

(async () => {
const client = new Client({
Expand All @@ -42,3 +43,8 @@ const { Client, Language, SpiralAbyss } = require("genshin-kit.js");
console.log(abyss);
})();
```

### More Information
- [API Reference](/api_reference/client)
- [Cache](/cache)
</Steps>
4 changes: 3 additions & 1 deletion apps/website/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Genshin-kit.js is a Node.js module that allows you to easily interact with the G

✅ 100% Promise-based\
✅ Completed typings \
✅ Actively maintained
✅ Cache support\
✅ Customizable cache\
✅ Easy to use

## Help

Expand Down
2 changes: 1 addition & 1 deletion packages/genshin-kit.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"license": "MIT",
"dependencies": {
"tslib": "^2.6.2",
"undici": "^6.16.1",
"undici": "^6.18.2",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
41 changes: 41 additions & 0 deletions packages/genshin-kit.js/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,65 @@ interface CacheOptions {
}

export class Client {
/**
* @type {DailyRewards}
* @description Daily rewards endpoint
*/
public dailyReward?: DailyRewards;

/**
* @type {GameRecordCard}
* @description Game record card endpoint
*/
public gameRecordCard?: GameRecordCard;

/**
* @type {SpiralAbyss}
* @description Spiral abyss endpoint
*/
public sprialAbyss?: SpiralAbyss;

/**
* @type {Activities}
* @description Genshin activity endpoint
*/
public genshinActivity?: Activities;

/**
* @type {Charcters}
* @description Genshin character endpoint
*/
public characters?: Charcters;


/**
* @type {GenshinUser}
* @description Genshin user endpoint
*/
public genshinUser?: GenshinUser;

/**
* @type {RealTimeNotes}
* @description Real time notes endpoint
*/
public realTimeNotes?: RealTimeNotes;

/**
* @type {TravelerDiary}
* @description Traveler diary endpoint
*/
public travelDiary?: TravelerDiary;

/**
* @type {RedeemCode}
* @description Redeem code endpoint
*/
public redeemCode?: RedeemCode;

/**
* @type {TCG}
* @description TCG endpoint
*/
public tcg?: TCG;

private options: ClientOptions & { cacheOptions: { maxAge: number; maxSize?: number } };
Expand Down
9 changes: 9 additions & 0 deletions packages/genshin-kit.js/src/client/clientCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export class ClientCache<V> extends Map<string, V> {
this.lifeMap = new Map();
}

/**
* @param {SweepFilterOptions<V>} fn
* @description Sweep the cache based on the filter function.
*/
public sweep(fn: SweepFilterOptions<V>): void {
if (this.size === 0) return;
for (const [key, value] of this) {
Expand All @@ -31,6 +35,11 @@ export class ClientCache<V> extends Map<string, V> {
}
}

/**
* @param {string} key
* @param {V} value
* @description Set a new value to the cache.
*/
public override set(key: string, value: V): this {
if (this.maxSize !== undefined && this.size >= this.maxSize) {
return this;
Expand Down
14 changes: 13 additions & 1 deletion packages/genshin-kit.js/src/client/clientCookieManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ export class ClientCookieManager {
}

/**
* @description Get the amount of cookies.
* @returns {number} - The amount of cookies.
*/
public get size(): number {
return this.cookie.length;
}

public setCookie(ltuid: string, ltoken: string): void {
this.cookie.push({ ltoken: ltoken, ltuid: ltuid });
}
Expand Down Expand Up @@ -62,10 +63,17 @@ export class ClientCookieManager {
this.cookie.splice(key - 1, 1);
}

/**
* @description Clear all cookies.
*/
public clear(): void {
this.cookie = [];
}

/**
* @description Get all cookies.
* @returns {cookie[]} - The cookies.
*/
public getAll(): cookie[] {
const cookie = [];

Expand All @@ -81,6 +89,10 @@ export class ClientCookieManager {
return cookie;
}

/**
* @param {string} customProfile - The custom profile to get the cookie.
* @description Get the ltoken and ltuid from browser cookie. (Install chrome-cookies-secure first)
*/
public async getBrowserCookie(customProfile: string): Promise<CookieStore> {
try {
await dynamic("chrome-cookies-secure");
Expand Down
1 change: 1 addition & 0 deletions packages/genshin-kit.js/src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function CookieFormatter(ltoken: string, ltuid: string): string {

/**
* @param {string} cookies - The cookie to parse.
* @returns {Record<string, string>} - The parsed cookie.
*/
export function CookieToObj(cookies: string): Record<string, string> {
const cookieObj: Record<string, string> = {};
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7104,10 +7104,10 @@ undici-types@~5.26.4:
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617"
integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==

undici@^6.16.1:
version "6.16.1"
resolved "https://registry.yarnpkg.com/undici/-/undici-6.16.1.tgz#ff8f49c56e5a8629f92ad2ce00d4841b9619b19e"
integrity sha512-NeNiTT7ixpeiL1qOIU/xTVpHpVP0svmI6PwoCKaMGaI5AsHOaRdwqU/f7Fi9eyU4u03nd5U/BC8wmRMnS9nqoA==
undici@^6.17.0:
version "6.17.0"
resolved "https://registry.yarnpkg.com/undici/-/undici-6.17.0.tgz#893d47b4027cff5409f11eb95d23196383ce9723"
integrity sha512-fs13QiDjPIzJ7gFAOal9CSG0c92rT2xw6MuMUJ4H30Eg5GCauLWYCCZA1tInjd6M4y+JZjVCCFr9pFpbhcC64w==

unified@^10.0.0:
version "10.1.2"
Expand Down

0 comments on commit 1bd866f

Please sign in to comment.