Skip to content

Commit

Permalink
chore: format & test
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Jan 20, 2024
1 parent 55cd7dd commit 6f31187
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 60 deletions.
2 changes: 1 addition & 1 deletion deno_dist/base/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('PayPayError', () => {
test('PayPay Fail', async () => {
expect(await paypay.login()).toStrictEqual({
success: false,
status: PayPayStatus.LoginFailed
status: PayPayStatus.LoginFailed,
})
})
})
32 changes: 14 additions & 18 deletions deno_dist/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PayPayError, PayPayStatus, isPassword, isPhone, isSuccess, isUuid } from '../index.ts'
import { PayPayError, PayPayStatus, isPassword, isPhone, isUuid } from '../index.ts'
import { createHeader } from '../headers/index.ts'
import type {
CreateLinkContext,
Expand Down Expand Up @@ -27,7 +27,6 @@ import {
parseReceiveLink,
parseRecoveryCode,
unparseRecoveryCode,
parseResultMessage,
parseAny,
} from '../utils/parse.ts'
import { randomUUID } from '../utils/uuid.ts'
Expand Down Expand Up @@ -211,9 +210,7 @@ export class PayPay {
'resultCode' in result['header'] &&
'resultMessage' in result['header']
) {
if (
result['header']['resultCode'] === 'S0001'
) {
if (result['header']['resultCode'] === 'S0001') {
// Refresh
await this.login({
uuid: this.uuid,
Expand Down Expand Up @@ -302,7 +299,7 @@ export class PayPay {

return parseCreateLink(result, true)
}

async getLink(link: string): Promise<ResponseGetLink> {
if (!this.isLogged()) {
throw new PayPayError('Do not logged in', 2).fire()
Expand Down Expand Up @@ -370,7 +367,7 @@ export class PayPay {
}

return parseReceiveLink(result, true)
}catch (_e) {
} catch (_e) {
throw new PayPayError('Invalid link', 1).fire()
}
}
Expand Down Expand Up @@ -410,7 +407,6 @@ export class PayPay {
}

async sendMoney(amount: number, external_id: string): Promise<ResponseBody> {

if (!this.isLogged()) {
throw new PayPayError('Do not logged in', 2).fire()
}
Expand All @@ -437,7 +433,7 @@ export class PayPay {
'https://www.paypay.ne.jp/app/v2/p2p-api/executeP2PSendMoney',
{
method: 'POST',
body: JSON.stringify(ctx),
body: JSON.stringify(ctx),
}
)

Expand All @@ -452,16 +448,19 @@ export class PayPay {
return parseAny(result, true)
}

async request(path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList'): Promise<ResponseAnyone> {
async request(
path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList'
): Promise<ResponseAnyone> {
if (!this.isLogged()) {
throw new PayPayError('Do not logged in', 2).fire()
}

const { response, result } = await this.baseFetch(
`https://www.paypay.ne.jp/app/v2/bff/${path}`,
{
method: 'GET'
})
{
method: 'GET',
}
)

if (!response.ok) {
return parseAny(result, false)
Expand Down Expand Up @@ -496,19 +495,16 @@ export class PayPayRecovery {
public password: string
public uuid: string

constructor(
code: string
) {
constructor(code: string) {
const object = unparseRecoveryCode(code)
this.phone = object.phone
this.password = object.password
this.uuid = object.uuid ?? randomUUID()
}

async recovery(): Promise<PayPay> {

const paypay = new PayPay(this.phone, this.password)

const result = await paypay.login({
uuid: this.uuid,
})
Expand Down
1 change: 1 addition & 0 deletions deno_dist/erorr/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('PayPayError', () => {
test('PayPayError Method', () => {
try {
throw error.fire()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: Error | any) {
expect(error.message).toBe('PayPaxError [1] : test')
}
Expand Down
14 changes: 7 additions & 7 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export type ReceiveLinkContext = {
}

export type SendMoneyContext = {
theme: string,
externalReceiverId: string,
amount: number,
requestId: string,
requestAt: string,
iosMinimumVersion: string,
theme: string
externalReceiverId: string
amount: number
requestId: string
requestAt: string
iosMinimumVersion: string
androidMinimumVersion: string
}

Expand Down Expand Up @@ -190,4 +190,4 @@ export type ResponseAnyone = {
[key: string]: Anyone
}
}
}
}
10 changes: 5 additions & 5 deletions deno_dist/utils/is.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ describe('Util', () => {
},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
oosugi: 'dounikashite',
},
}

const invalidSuccess = {
header: {
resultCode: 'S0001',
resultMessage: 'Invalid anyone',
},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
oosugi: 'dounikashite',
},
}

expect(util.isSuccess(validSuccess)).toBeTruthy()
Expand Down
7 changes: 3 additions & 4 deletions deno_dist/utils/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,16 @@ export function isUuid(uuid: string): boolean {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(uuid)
}


export function isSuccess(raw: {
header: {
resultCode: string
resultMessage: string
},
}
payload: Anyone
}): boolean {
try {
return raw.header.resultCode === 'S0000'
}catch (e) {
} catch (e) {
return false
}
}
}
18 changes: 10 additions & 8 deletions deno_dist/utils/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe('Util', () => {
const password = 'ctkpaarR2'
const uuid = 'a9b3d5c6-7d8e-9f0a-bcde-fghijk'

expect(util.parseRecoveryCode(phone, password, uuid)).toBe('eyJhbGcMDkwMTkxOTQ1NDU=.Y3RrcGFhclIy.YTliM2Q1YzYtN2Q4ZS05ZjBhLWJjZGUtZmdoaWpr')
expect(util.parseRecoveryCode(phone, password, uuid)).toBe(
'eyJhbGcMDkwMTkxOTQ1NDU=.Y3RrcGFhclIy.YTliM2Q1YzYtN2Q4ZS05ZjBhLWJjZGUtZmdoaWpr'
)
})

test('unparseRecoveryCode', () => {
Expand All @@ -37,8 +39,8 @@ describe('Util', () => {
},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
oosugi: 'dounikashite',
},
}

expect(util.parseBalanceContext(failResponse, false)).toStrictEqual({
Expand All @@ -47,7 +49,7 @@ describe('Util', () => {
total: 0,
currency: 'JPY',
updated_at: new Date(0).toISOString(),
raw: failResponse
raw: failResponse,
})
})

Expand All @@ -59,8 +61,8 @@ describe('Util', () => {
},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
oosugi: 'dounikashite',
},
}

expect(util.parseResultMessage(eg1)).toBe('Example 1')
Expand All @@ -69,8 +71,8 @@ describe('Util', () => {
header: {},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
oosugi: 'dounikashite',
},
}

expect(util.parseResultMessage(eg2)).toBe('unknown')
Expand Down
22 changes: 15 additions & 7 deletions deno_dist/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { PayPayError, isSuccess } from '../index.ts'
import type { Anyone, ResponseAnyone, ResponseBalance, ResponseCreateLink, ResponseGetLink, ResponseReceiveLink, ResponseUserInfo } from '../types.ts'
import type {
Anyone,
ResponseAnyone,
ResponseBalance,
ResponseCreateLink,
ResponseGetLink,
ResponseReceiveLink,
ResponseUserInfo,
} from '../types.ts'

export function parseCookieFromMap(map: Map<string, string>): string {
return Array.from(map.entries())
Expand Down Expand Up @@ -62,7 +70,7 @@ export function parseBalanceContext(result: Anyone, success: boolean): ResponseB
updated_at: new Date(0).toISOString(),
raw: result,
}
}catch (_e) {
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
Expand Down Expand Up @@ -92,7 +100,7 @@ export function parseUserInfoContext(result: Anyone, success: boolean): Response
external_id: result.payload.external_id ?? 'unknown',
raw: result,
}
}catch (_e) {
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
Expand Down Expand Up @@ -124,7 +132,7 @@ export function parseCreateLink(result: Anyone, success: boolean): ResponseCreat
expiry: result.payload.expiry ?? new Date(0).toISOString(),
raw: result,
}
}catch (_e) {
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
Expand Down Expand Up @@ -160,7 +168,7 @@ export function parseGetLink(result: Anyone, success: boolean): ResponseGetLink
raw: result,
}
}

return {
success: success && isSuccess(result),
message: parseResultMessage(result),
Expand All @@ -179,7 +187,7 @@ export function parseGetLink(result: Anyone, success: boolean): ResponseGetLink
photo_url: result.payload.sender.photoUrl ?? 'unknown',
raw: result,
}
}catch (_e) {
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
Expand Down Expand Up @@ -237,4 +245,4 @@ export function parseAny(result: Anyone, success: boolean): ResponseAnyone {
raw: result,
}
}
}
}
1 change: 0 additions & 1 deletion deno_dist/utils/uuid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isUuid } from './is.ts'
import { randomUUID } from './uuid.ts'

describe('UUIDError', () => {

test('UUID', async () => {
expect(isUuid(randomUUID())).toBeTruthy()
})
Expand Down
11 changes: 6 additions & 5 deletions deno_dist/utils/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export function randomUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (a) => {
const r = (new Date().getTime() + Math.random() * 16) % 16 | 0, v = a == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (a) => {
const r = (new Date().getTime() + Math.random() * 16) % 16 | 0,
v = a == 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "paypax",
"version": "1.7.2",
"version": "1.7.3",
"description": "Library for automate PayPay operations",
"scripts": {
"format": "prettier --write ./src/*.{ts,tsx} ./src/**/*.{ts,tsx}",
Expand Down
5 changes: 2 additions & 3 deletions src/base/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PayPayError, PayPayStatus, isPassword, isPhone, isSuccess, isUuid } from '..'
import { PayPayError, PayPayStatus, isPassword, isPhone, isUuid } from '..'
import { createHeader } from '../headers'
import type {
CreateLinkContext,
Expand Down Expand Up @@ -27,7 +27,6 @@ import {
parseReceiveLink,
parseRecoveryCode,
unparseRecoveryCode,
parseResultMessage,
parseAny,
} from '../utils/parse'
import { randomUUID } from '../utils/uuid'
Expand Down Expand Up @@ -443,7 +442,7 @@ export class PayPay {
}

if (result.header.resultCode === 'S9999') {
throw new PayPayError("You're not friends with user", 1).fire()
throw new PayPayError('You\'re not friends with user', 1).fire()
}

return parseAny(result, true)
Expand Down
1 change: 1 addition & 0 deletions src/erorr/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe('PayPayError', () => {
test('PayPayError Method', () => {
try {
throw error.fire()
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: Error | any) {
expect(error.message).toBe('PayPaxError [1] : test')
}
Expand Down

0 comments on commit 6f31187

Please sign in to comment.