Skip to content

Commit

Permalink
fix: add message prop on forget context
Browse files Browse the repository at this point in the history
  • Loading branch information
EdamAme-x committed Jan 20, 2024
1 parent 6fcc072 commit 1eac344
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 57 deletions.
36 changes: 10 additions & 26 deletions deno_dist/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
LoginContext,
OTP,
ReceiveLinkContext,
ResponseAnyone,
ResponseBalance,
ResponseBody,
ResponseCreateLink,
Expand All @@ -26,6 +27,8 @@ import {
parseReceiveLink,
parseRecoveryCode,
unparseRecoveryCode,
parseResultMessage,
parseAny,
} from '../utils/parse.ts'
import { randomUUID } from '../utils/uuid.ts'

Expand Down Expand Up @@ -400,16 +403,10 @@ export class PayPay {
)

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

return {
success: isSuccess(result),
raw: result
}
return parseAny(result, true)
}

async sendMoney(amount: number, external_id: string): Promise<ResponseBody> {
Expand Down Expand Up @@ -445,24 +442,17 @@ export class PayPay {
)

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

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

return {
success: isSuccess(result),
...result.payload,
raw: result
}
return parseAny(result, true)
}

async request(path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList') {
async request(path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList'): Promise<ResponseAnyone> {
if (!this.isLogged()) {
throw new PayPayError('Do not logged in', 2).fire()
}
Expand All @@ -474,16 +464,10 @@ export class PayPay {
})

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

return {
success: isSuccess(result),
raw: result
}
return parseAny(result, true)
}

public getRecoveryCode(): string {
Expand Down
10 changes: 10 additions & 0 deletions deno_dist/status/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// PayPay Status Test
import { PayPayStatus } from './index.ts'

describe('Status', () => {
test('not include space', () => {
for (const Status of Object.values(PayPayStatus)) {
expect(Status).not.toMatch(/\s/)
}
})
})
14 changes: 14 additions & 0 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,18 @@ export type ResponseReceiveLink = {
[key: string]: Anyone
}
}
}

export type ResponseAnyone = {
success: boolean
message: string
raw: {
header: {
resultCode: string
resultMessage: string
}
payload: {
[key: string]: Anyone
}
}
}
29 changes: 28 additions & 1 deletion deno_dist/utils/is.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// PayPay Error Test
// PayPay is Test
import * as util from './is.ts'

describe('Util', () => {
Expand Down Expand Up @@ -28,4 +28,31 @@ describe('Util', () => {
expect(util.isUuid(invalidUuid)).toBeFalsy()
expect(util.isUuid(invalidUuid2)).toBeFalsy()
})

test('isSuccess', () => {
const validSuccess = {
header: {
resultCode: 'S0000',
resultMessage: 'Success',
},
payload: {
paypay: 'resultCode',
oosugi: 'dounikashite'
}
}

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

expect(util.isSuccess(validSuccess)).toBeTruthy()
expect(util.isSuccess(invalidSuccess)).toBeFalsy()
})
})
2 changes: 1 addition & 1 deletion deno_dist/utils/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// PayPay Error Test
// PayPay Parse Test
import * as util from './parse.ts'

describe('Util', () => {
Expand Down
18 changes: 17 additions & 1 deletion deno_dist/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PayPayError, isSuccess } from '../index.ts'
import type { Anyone, 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 @@ -221,4 +221,20 @@ export function parseReceiveLink(result: Anyone, success: boolean): ResponseRece
raw: result,
}
}
}

export function parseAny(result: Anyone, success: boolean): ResponseAnyone {
try {
return {
success: success && isSuccess(result),
message: parseResultMessage(result),
raw: result,
}
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
raw: result,
}
}
}
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.0",
"version": "1.7.2",
"description": "Library for automate PayPay operations",
"scripts": {
"format": "prettier --write ./src/*.{ts,tsx} ./src/**/*.{ts,tsx}",
Expand Down
36 changes: 10 additions & 26 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
LoginContext,
OTP,
ReceiveLinkContext,
ResponseAnyone,
ResponseBalance,
ResponseBody,
ResponseCreateLink,
Expand All @@ -26,6 +27,8 @@ import {
parseReceiveLink,
parseRecoveryCode,
unparseRecoveryCode,
parseResultMessage,
parseAny,
} from '../utils/parse'
import { randomUUID } from '../utils/uuid'

Expand Down Expand Up @@ -400,16 +403,10 @@ export class PayPay {
)

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

return {
success: isSuccess(result),
raw: result
}
return parseAny(result, true)
}

async sendMoney(amount: number, external_id: string): Promise<ResponseBody> {
Expand Down Expand Up @@ -445,24 +442,17 @@ export class PayPay {
)

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

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

return {
success: isSuccess(result),
...result.payload,
raw: result
}
return parseAny(result, true)
}

async request(path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList') {
async request(path: 'getProfileDisplayInfo' | 'getPay2BalanceHistory' | 'getPaymentMethodList'): Promise<ResponseAnyone> {
if (!this.isLogged()) {
throw new PayPayError('Do not logged in', 2).fire()
}
Expand All @@ -474,16 +464,10 @@ export class PayPay {
})

if (!response.ok) {
return {
success: false,
raw: result
}
return parseAny(result, false)
}

return {
success: isSuccess(result),
raw: result
}
return parseAny(result, true)
}

public getRecoveryCode(): string {
Expand Down
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,18 @@ export type ResponseReceiveLink = {
[key: string]: Anyone
}
}
}

export type ResponseAnyone = {
success: boolean
message: string
raw: {
header: {
resultCode: string
resultMessage: string
}
payload: {
[key: string]: Anyone
}
}
}
18 changes: 17 additions & 1 deletion src/utils/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PayPayError, isSuccess } from '..'
import type { Anyone, 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 @@ -221,4 +221,20 @@ export function parseReceiveLink(result: Anyone, success: boolean): ResponseRece
raw: result,
}
}
}

export function parseAny(result: Anyone, success: boolean): ResponseAnyone {
try {
return {
success: success && isSuccess(result),
message: parseResultMessage(result),
raw: result,
}
} catch (_e) {
return {
success: false,
message: parseResultMessage(result),
raw: result,
}
}
}

0 comments on commit 1eac344

Please sign in to comment.