Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Jan 20, 2024
1 parent 1eac344 commit 55cd7dd
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/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,
})
})
})
31 changes: 14 additions & 17 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,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 +300,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 +368,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 +408,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 +434,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 @@ -446,22 +443,25 @@ 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)
}

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 +496,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
14 changes: 7 additions & 7 deletions src/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 src/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 src/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 src/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 src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { PayPayError, isSuccess } from '..'
import type { Anyone, ResponseAnyone, ResponseBalance, ResponseCreateLink, ResponseGetLink, ResponseReceiveLink, ResponseUserInfo } from '../types'
import type {
Anyone,
ResponseAnyone,
ResponseBalance,
ResponseCreateLink,
ResponseGetLink,
ResponseReceiveLink,
ResponseUserInfo,
} from '../types'

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 src/utils/uuid.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { isUuid } from './is'
import { randomUUID } from './uuid'

describe('UUIDError', () => {

test('UUID', async () => {
expect(isUuid(randomUUID())).toBeTruthy()
})
Expand Down
11 changes: 6 additions & 5 deletions src/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)
})
}

0 comments on commit 55cd7dd

Please sign in to comment.