From 33ccd7929a7ef3a80d6187edc0832fa0dc89ed98 Mon Sep 17 00:00:00 2001 From: Yiin Date: Tue, 7 Apr 2020 06:00:14 +0300 Subject: [PATCH 1/2] Add typings for api/contacts and api/user --- lib/api/contacts.js | 70 -------------------------- lib/api/contacts.ts | 118 ++++++++++++++++++++++++++++++++++++++++++++ lib/api/user.js | 68 ------------------------- lib/api/user.ts | 94 +++++++++++++++++++++++++++++++++++ 4 files changed, 212 insertions(+), 138 deletions(-) delete mode 100644 lib/api/contacts.js create mode 100644 lib/api/contacts.ts delete mode 100644 lib/api/user.js create mode 100644 lib/api/user.ts diff --git a/lib/api/contacts.js b/lib/api/contacts.js deleted file mode 100644 index 239476ea..00000000 --- a/lib/api/contacts.js +++ /dev/null @@ -1,70 +0,0 @@ -export const queryContacts = ({ Page = 0, PageSize = 1000, LabelID } = {}) => ({ - url: 'contacts', - method: 'get', - params: { Page, PageSize, LabelID } -}); - -export const queryContactExport = ({ Page = 0, PageSize = 50, LabelID } = {}) => ({ - url: 'contacts/export', - method: 'get', - params: { Page, PageSize, LabelID } -}); - -export const getContact = (contactID) => ({ - url: `contacts/${contactID}`, - method: 'get' -}); - -export const addContacts = ({ Contacts, Overwrite, Labels, timeout } = {}) => ({ - url: 'contacts', - method: 'post', - data: { Contacts, Overwrite, Labels }, - timeout -}); - -export const updateContact = (contactID, { Cards } = {}) => ({ - url: `contacts/${contactID}`, - method: 'put', - data: { Cards } -}); - -export const labelContacts = ({ LabelID, ContactIDs } = {}) => ({ - url: 'contacts/label', - method: 'put', - data: { LabelID, ContactIDs } -}); - -export const unLabelContacts = ({ LabelID, ContactIDs } = {}) => ({ - url: 'contacts/unlabel', - method: 'put', - data: { LabelID, ContactIDs } -}); - -export const deleteContacts = (IDs) => ({ - url: 'contacts/delete', - method: 'put', - data: { IDs } -}); - -export const clearContacts = () => ({ - url: 'contacts', - method: 'delete' -}); - -export const queryContactEmails = ({ Page, PageSize, Email, LabelID } = {}) => ({ - url: 'contacts/emails', - method: 'get', - params: { Page, PageSize, Email, LabelID } -}); - -export const labelContactEmails = ({ LabelID, ContactEmailIDs } = {}) => ({ - url: 'contacts/emails/label', - method: 'put', - data: { LabelID, ContactEmailIDs } -}); - -export const unLabelContactEmails = ({ LabelID, ContactEmailIDs } = {}) => ({ - url: 'contacts/emails/unlabel', - method: 'put', - data: { LabelID, ContactEmailIDs } -}); diff --git a/lib/api/contacts.ts b/lib/api/contacts.ts new file mode 100644 index 00000000..a1c834a2 --- /dev/null +++ b/lib/api/contacts.ts @@ -0,0 +1,118 @@ +export const queryContacts = ({ + Page = 0, + PageSize = 1000, + LabelID +}: { + Page?: number; + PageSize?: number; + LabelID?: string; +} = {}) => ({ + url: 'contacts', + method: 'get', + params: { Page, PageSize, LabelID } +}); + +export const queryContactExport = ({ + Page = 0, + PageSize = 50, + LabelID +}: { + Page?: number; + PageSize?: number; + LabelID?: string; +} = {}) => ({ + url: 'contacts/export', + method: 'get', + params: { Page, PageSize, LabelID } +}); + +export const getContact = (contactID: string) => ({ + url: `contacts/${contactID}`, + method: 'get' +}); + +interface Card { + Type: number; + Data: string; + Signature: string | null; +} + +export const addContacts = ({ + Contacts, + Overwrite, + Labels, + timeout +}: { + Contacts: { + Cards: Card[]; + }[]; + Overwrite: 0 | 1; + Labels?: number; + timeout?: number; +}) => ({ + url: 'contacts', + method: 'post', + data: { Contacts, Overwrite, Labels }, + timeout +}); + +export const updateContact = (contactID: string, { Cards }: { Cards: Card[] }) => ({ + url: `contacts/${contactID}`, + method: 'put', + data: { Cards } +}); + +export const labelContacts = ({ LabelID, ContactIDs }: { LabelID: string; ContactIDs: string[] }) => ({ + url: 'contacts/label', + method: 'put', + data: { LabelID, ContactIDs } +}); + +export const unLabelContacts = ({ LabelID, ContactIDs }: { LabelID: string; ContactIDs: string[] }) => ({ + url: 'contacts/unlabel', + method: 'put', + data: { LabelID, ContactIDs } +}); + +export const deleteContacts = (IDs: string[]) => ({ + url: 'contacts/delete', + method: 'put', + data: { IDs } +}); + +export const clearContacts = () => ({ + url: 'contacts', + method: 'delete' +}); + +export const queryContactEmails = ({ + Page, + PageSize, + Email, + LabelID +}: { + Page?: number; + PageSize?: number; +} & ({ + Email?: string; + LabelID?: never; +} | { + Email?: never; + LabelID?: string; +}) = {}) => ({ + url: 'contacts/emails', + method: 'get', + params: { Page, PageSize, Email, LabelID } +}); + +export const labelContactEmails = ({ LabelID, ContactEmailIDs }: { LabelID: string; ContactEmailIDs: string[] }) => ({ + url: 'contacts/emails/label', + method: 'put', + data: { LabelID, ContactEmailIDs } +}); + +export const unLabelContactEmails = ({ LabelID, ContactEmailIDs }: { LabelID: string; ContactEmailIDs: string[] }) => ({ + url: 'contacts/emails/unlabel', + method: 'put', + data: { LabelID, ContactEmailIDs } +}); diff --git a/lib/api/user.js b/lib/api/user.js deleted file mode 100644 index 73abef11..00000000 --- a/lib/api/user.js +++ /dev/null @@ -1,68 +0,0 @@ -export const getUser = () => ({ - url: 'users', - method: 'get' -}); - -export const queryCreateUser = ({ Auth, Email, Token, TokenType, Type, Username }) => ({ - url: 'users', - method: 'post', - data: { Auth, Email, Token, TokenType, Type, Username } -}); - -export const queryUnlock = () => ({ - url: 'users/unlock', - method: 'put' -}); - -export const deleteUser = (data) => ({ - url: 'users/delete', - method: 'put', - data -}); - -export const unlockPasswordChanges = () => ({ - url: 'users/password', - method: 'put' -}); - -export const lockSensitiveSettings = () => ({ - url: 'users/lock', - method: 'put' -}); - -export const getHumanVerificationMethods = () => ({ - url: 'users/human', - method: 'get' -}); - -export const queryVerificationCode = (Type, Destination) => ({ - url: 'users/code', - method: 'post', - data: { Type, Destination } -}); - -export const queryCheckUsernameAvailability = (Name) => ({ - url: 'users/available', - method: 'get', - params: { Name } -}); - -/** - * @param {1 | 2} Type 1 = mail, 2 = VPN - */ -export const queryDirectSignupStatus = (Type) => ({ - url: 'users/direct', - method: 'get', - params: { Type } -}); - -/** - * @param {string} Token - * @param {'email'|'sms'|'invite'|'coupon'|'payment'} TokenType - * @param {1 | 2} Type 1 = mail, 2 = VPN - */ -export const queryCheckVerificationCode = (Token, TokenType, Type) => ({ - url: 'users/check', - method: 'put', - data: { Token, TokenType, Type } -}); diff --git a/lib/api/user.ts b/lib/api/user.ts new file mode 100644 index 00000000..0b79d110 --- /dev/null +++ b/lib/api/user.ts @@ -0,0 +1,94 @@ +export const getUser = () => ({ + url: 'users', + method: 'get' +}); + +export const queryCreateUser = (data: { + Username: string; + Email: string; + Token: string; + TokenType: 'captcha' | 'email' | 'sms' | 'invite' | 'payment'; + Type: number; // 1 = mail, 2 = VPN + Auth: { + Version: number; + ModulusID: string; + Salt: string; + Verifier: string; + }; + Referrer?: string; + Payload?: { + [key: string]: string; + }; + Salt?: string; +}) => ({ + url: 'users', + method: 'post', + data +}); + +export const queryUnlock = () => ({ + url: 'users/unlock', + method: 'put' +}); + +export const deleteUser = (data: { + ClientEphemeral: string; + ClientProof: string; + SRPSession: string; + TwoFactorCode: number; +}) => ({ + url: 'users/delete', + method: 'put', + data +}); + +export const unlockPasswordChanges = () => ({ + url: 'users/password', + method: 'put' +}); + +export const lockSensitiveSettings = () => ({ + url: 'users/lock', + method: 'put' +}); + +export const getHumanVerificationMethods = () => ({ + url: 'users/human', + method: 'get' +}); + +export const queryVerificationCode = ( + Type: 'email' | 'sms', + Destination: { + Address: string; + Phone: string; + } +) => ({ + url: 'users/code', + method: 'post', + data: { Type, Destination } +}); + +export const queryCheckUsernameAvailability = (Name: string) => ({ + url: 'users/available', + method: 'get', + params: { Name } +}); + +export const queryDirectSignupStatus = ( + Type: number // 1 = mail, 2 = VPN +) => ({ + url: 'users/direct', + method: 'get', + params: { Type } +}); + +export const queryCheckVerificationCode = ( + Token: string, + TokenType: 'email' | 'sms' | 'invite' | 'coupon' | 'payment', + Type: number // 1 = mail, 2 = VPN +) => ({ + url: 'users/check', + method: 'put', + data: { Token, TokenType, Type } +}); From 3ed234331410ab6e54cfa0280f30919234629c22 Mon Sep 17 00:00:00 2001 From: Yiin Date: Tue, 7 Apr 2020 18:57:11 +0300 Subject: [PATCH 2/2] Review --- lib/api/user.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/api/user.ts b/lib/api/user.ts index 0b79d110..77db5625 100644 --- a/lib/api/user.ts +++ b/lib/api/user.ts @@ -8,7 +8,7 @@ export const queryCreateUser = (data: { Email: string; Token: string; TokenType: 'captcha' | 'email' | 'sms' | 'invite' | 'payment'; - Type: number; // 1 = mail, 2 = VPN + Type: 1 | 2; // 1 = mail, 2 = VPN Auth: { Version: number; ModulusID: string; @@ -61,6 +61,9 @@ export const queryVerificationCode = ( Type: 'email' | 'sms', Destination: { Address: string; + Phone?: never; + } | { + Address?: never; Phone: string; } ) => ({ @@ -76,7 +79,7 @@ export const queryCheckUsernameAvailability = (Name: string) => ({ }); export const queryDirectSignupStatus = ( - Type: number // 1 = mail, 2 = VPN + Type: 1 | 2 // 1 = mail, 2 = VPN ) => ({ url: 'users/direct', method: 'get', @@ -86,7 +89,7 @@ export const queryDirectSignupStatus = ( export const queryCheckVerificationCode = ( Token: string, TokenType: 'email' | 'sms' | 'invite' | 'coupon' | 'payment', - Type: number // 1 = mail, 2 = VPN + Type: 1 | 2 // 1 = mail, 2 = VPN ) => ({ url: 'users/check', method: 'put',