Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Bump to v2 #67

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0ca667c
chore: update author and add repository field in package.json
wdzeng Jul 29, 2023
9763d7b
feat: implement v2
wdzeng Jul 21, 2023
41a83b8
docs: update docs and demo pictures for v2
wdzeng Jul 21, 2023
d22e820
feat: update Dockerfile for v2
wdzeng Jul 21, 2023
7350898
ci/cd: update CI/CD for v2
wdzeng Jul 21, 2023
4fdb853
Bump to v2.0.0-alpha.0
wdzeng Jul 21, 2023
14e22fb
fix: incorrect checkin list length for history command JSON output
wdzeng Jul 21, 2023
ce01cc6
Bump to v2.0.0-alpha.1
wdzeng Jul 21, 2023
0295dfa
fix: some unknown argument bypass
wdzeng Jul 22, 2023
bfda81e
fix: redundant linefeed for `history` command JSON output
wdzeng Jul 22, 2023
7ee0c50
build: add `non-cache` on `apk add` in Dockerfile
wdzeng Jul 23, 2023
1e28a74
docs: state that do not log out after getting cookie in instructions
wdzeng Jul 25, 2023
e231656
Bump to v2.0.0-alpha.2
wdzeng Jul 25, 2023
481c49d
docs: fix `history` command output format instruction
wdzeng Jul 25, 2023
18ef0a9
docs: state that all CLIs in demos are omitting usage of `-v` and `-c`
wdzeng Jul 25, 2023
b883a9c
fix: output color is yellow for command `balance`
wdzeng Jul 25, 2023
83c960d
refact: replace loglevel with our own implementation
wdzeng Jul 25, 2023
74463da
docs: add instruction about reporting issues for v2
wdzeng Jul 28, 2023
7fb0a2f
refact: remove global variables in index.ts
wdzeng Jul 29, 2023
bc4bfea
refact: separate CLI and internal API
wdzeng Jul 29, 2023
e888b3d
build: replace webpack with esbuild
wdzeng Jul 29, 2023
eb842bf
feat: add more error handling about expired cookie
wdzeng Sep 2, 2023
e919e4d
chore: upgrade dependencies and package manager and use nolyfill
wdzeng Sep 2, 2023
4e27ba8
refact: api types
wdzeng Sep 2, 2023
ff611b9
style: update src/cli/index.js concerning process exit styles
wdzeng Sep 2, 2023
217aba4
docs: update README for v2 release
wdzeng Sep 5, 2023
3ac5a7a
feat: replace fetch with axios
wdzeng Sep 5, 2023
8a6948a
chore: update version replacement comment
wdzeng Sep 5, 2023
4b3996f
refact: use @commander-js/extra-typings and split code under cli
wdzeng Sep 5, 2023
3cf17dc
refact: improve error handling
wdzeng Sep 30, 2023
16111e7
chore: upgrade dependencies and default package manager
wdzeng Sep 30, 2023
ff85bc3
ci/cd: merge workflow unittests.yml into publish.yml
wdzeng Sep 30, 2023
5c78944
chore: add fake test script
wdzeng Sep 30, 2023
1f56446
Bump to v2.0.0-alpha.3
wdzeng Sep 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add more error handling about expired cookie
wdzeng committed Sep 5, 2023
commit eb842bf1c8efd1fadddd5db1f4c374a2cc80a121
1 change: 0 additions & 1 deletion src/api/errors.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,6 @@ export class UserNotLoggedInError extends CustomError {
}
}

// TODO: Use ShopeeError if status code is 2XX but response body contains error messages.
export class ShopeeError extends CustomError {
constructor(
public readonly code: number,
37 changes: 25 additions & 12 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import assert from 'node:assert'

import { parseCookie } from '@/api/cookie'
import { InvalidCookieError, UserNotLoggedInError } from '@/api/errors'
import { InvalidCookieError, ShopeeError, UserNotLoggedInError } from '@/api/errors'
import type { CheckinResponse } from '@/api/types/checkin'
import type { CoinsResponse, UnknownCoinsResponse } from '@/api/types/coins'
import type { CoinsResponse } from '@/api/types/coins'
import type { SettingsResponse } from '@/api/types/settings'

export interface CheckinHistory {
@@ -13,6 +15,21 @@ export interface CheckinHistory {
export default class ShopeeBot {
constructor(private readonly cookie: string) {}

private handleErrorResponse(responseData: object): void {
if (
'code' in responseData &&
typeof responseData.code === 'number' &&
'msg' in responseData &&
typeof responseData.msg === 'string'
) {
if (responseData.code === 401) {
throw new UserNotLoggedInError()
} else if (responseData.code !== 0) {
throw new ShopeeError(responseData.code, `Shopee server: ${responseData.msg}`)
}
}
}

private async getCoinsApiResponseBody(): Promise<CoinsResponse> {
const url = 'https://shopee.tw/mkt/coins/api/v1/cs/coins'
const fetchResult = await fetch(url, {
@@ -34,16 +51,8 @@ export default class ShopeeBot {
}
})
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const result: UnknownCoinsResponse = await fetchResult.json()
if ('code' in result) {
if (result.code === 401) {
throw new InvalidCookieError(`Shopee server: ${result.msg}`)
}

// Unexpected error.
throw new Error(`Shopee server: ${result.msg}`)
}

const result: CoinsResponse = await fetchResult.json()
this.handleErrorResponse(result)
return result
}

@@ -74,6 +83,8 @@ export default class ShopeeBot {
})
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const responseBody: CheckinResponse = await fetchResult.json()
this.handleErrorResponse(responseBody)
assert('data' in responseBody)
return responseBody.data.success ? responseBody.data.increase_coins : false
}

@@ -104,6 +115,8 @@ export default class ShopeeBot {
})
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const body: SettingsResponse = await fetchResult.json()
this.handleErrorResponse(body)
assert('data' in body)

if (body.data.userid === '-1') {
throw new UserNotLoggedInError()
2 changes: 1 addition & 1 deletion src/api/types/checkin.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
export interface CheckinResponse {
code: number
msg: string
data: Data
data?: Data
}

export interface Data {
7 changes: 0 additions & 7 deletions src/api/types/coins.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
// Response from API https://shopee.tw/mkt/coins/api/v1/cs/coins

export type UnknownCoinsResponse = CoinsResponse | ErrorCoinsResponse

export interface CoinsResponse {
coins: number
logid: string
ts: number
userid: string
username: string
}

export interface ErrorCoinsResponse {
code: number // 401 indicated invalid cookie
msg: string
}
2 changes: 1 addition & 1 deletion src/api/types/settings.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
export interface SettingsResponse {
code: number
msg: string
data: Data
data?: Data
}

interface Data {