From f27d4dd9c69b29acde4ab1a1b904007f0481ef0a Mon Sep 17 00:00:00 2001 From: Aviv Ben Shahar Date: Fri, 7 Jun 2024 15:20:53 +0300 Subject: [PATCH] feat: support input plamform --- package-lock.json | 18 +++++++++--------- package.json | 4 ++-- src/common/utils.ts | 4 ++++ src/main/contacts.ts | 19 +++++++++++++++++-- src/main/open-whatsapp.ts | 7 ++----- src/models/contact-payload.model.ts | 2 ++ src/models/platform.model.ts | 2 ++ 7 files changed, 38 insertions(+), 18 deletions(-) create mode 100644 src/common/utils.ts create mode 100644 src/models/platform.model.ts diff --git a/package-lock.json b/package-lock.json index fd86d6c..93e6404 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6,10 +6,10 @@ "packages": { "": { "name": "alfred-open-whatsapp", - "version": "2.0.3", + "version": "2.1.0", "license": "MIT", "dependencies": { - "fast-alfred": "^1.3.3", + "fast-alfred": "^1.3.4", "fuse.js": "^7.0.0", "libphonenumber-js": "^1.11.2", "node-mac-contacts": "^1.7.2" @@ -5098,9 +5098,9 @@ } }, "node_modules/fast-alfred": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/fast-alfred/-/fast-alfred-1.3.3.tgz", - "integrity": "sha512-PJkd0ApcbDNcUCFdq6wSvPDF3XPmSz6zxnAL4GqL06CbbhrVQM5LQPw9kTK5aJUKBkNoY2plf9imlBn5Q3VL7g==", + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/fast-alfred/-/fast-alfred-1.3.4.tgz", + "integrity": "sha512-uEpYQef8xDhHghSx3RXw728nQofBUQx56O1RGiTJAiU+fPASnwPsNA476GPl733JjWGwZuWUO8FoG+jSOXUKNw==", "dependencies": { "@nestjs/common": "^10.3.9", "@nestjs/core": "^10.3.9", @@ -16334,9 +16334,9 @@ } }, "fast-alfred": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/fast-alfred/-/fast-alfred-1.3.3.tgz", - "integrity": "sha512-PJkd0ApcbDNcUCFdq6wSvPDF3XPmSz6zxnAL4GqL06CbbhrVQM5LQPw9kTK5aJUKBkNoY2plf9imlBn5Q3VL7g==", + "version": "1.3.4", + "resolved": "https://registry.npmjs.org/fast-alfred/-/fast-alfred-1.3.4.tgz", + "integrity": "sha512-uEpYQef8xDhHghSx3RXw728nQofBUQx56O1RGiTJAiU+fPASnwPsNA476GPl733JjWGwZuWUO8FoG+jSOXUKNw==", "requires": { "@nestjs/common": "^10.3.9", "@nestjs/core": "^10.3.9", @@ -21726,4 +21726,4 @@ "dev": true } } -} \ No newline at end of file +} diff --git a/package.json b/package.json index 2628892..803c68e 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "test": "jest" }, "dependencies": { - "fast-alfred": "^1.3.3", + "fast-alfred": "^1.3.4", "fuse.js": "^7.0.0", "libphonenumber-js": "^1.11.2", "node-mac-contacts": "^1.7.2" @@ -82,4 +82,4 @@ "publishConfig": { "registry": "https://registry.npmjs.org/" } -} \ No newline at end of file +} diff --git a/src/common/utils.ts b/src/common/utils.ts new file mode 100644 index 0000000..d1ed695 --- /dev/null +++ b/src/common/utils.ts @@ -0,0 +1,4 @@ +import { exec } from 'node:child_process' +import { promisify } from 'node:util' + +export const execPromise = promisify(exec) diff --git a/src/main/contacts.ts b/src/main/contacts.ts index 1a00655..38ce2a1 100644 --- a/src/main/contacts.ts +++ b/src/main/contacts.ts @@ -5,12 +5,23 @@ import { DEFAULT_MAX_RESULTS_COUNT } from '@common/constants.js' import { Variables } from '@common/variables.js' import type { ContactPayload } from '@models/contact-payload.model.js' import type { IContact } from '@models/contact.model.js' +import { SUPPORTED_PLATFORMS, type SupportedPlatform } from '@models/platform.model.js' import { getContacts } from '@services/contacts.service.js' import { searchContacts } from '@services/search.service.js' ;(() => { const alfredClient = new FastAlfred() + const [searchTerm, platform] = alfredClient.inputs + + if (!SUPPORTED_PLATFORMS.includes(platform as SupportedPlatform)) { + const errorMassage = `Unsupported platform: ${platform}, Supported platforms: ${SUPPORTED_PLATFORMS.join(', ')}` + const error = new Error(errorMassage) + + alfredClient.error(error) + return + } + const countryCode: CountryCode = alfredClient.env.getEnv(Variables.COUNTRY_CODE, { defaultValue: 'US', }) @@ -22,11 +33,15 @@ import { searchContacts } from '@services/search.service.js' const contacts: IContact[] = getContacts(alfredClient) - const filteredContacts = searchContacts(contacts, alfredClient.input, sliceAmount) + const filteredContacts = searchContacts(contacts, searchTerm, sliceAmount) const items: AlfredScriptFilter['items'] = filteredContacts.map( ({ firstName, lastName, phoneNumbers }: IContact) => { - const payload: ContactPayload = { phoneNumber: phoneNumbers[0], countryCode } + const payload: ContactPayload = { + phoneNumber: phoneNumbers[0], + countryCode, + platform: platform as SupportedPlatform, + } return { title: `${firstName} ${lastName}`, diff --git a/src/main/open-whatsapp.ts b/src/main/open-whatsapp.ts index 25ab3d4..1fa8493 100644 --- a/src/main/open-whatsapp.ts +++ b/src/main/open-whatsapp.ts @@ -1,12 +1,9 @@ import { FastAlfred } from 'fast-alfred' import type { PhoneNumber } from 'libphonenumber-js' import { parsePhoneNumber } from 'libphonenumber-js' -import { exec } from 'node:child_process' -import { promisify } from 'node:util' +import { execPromise } from '@common/utils.js' import type { ContactPayload } from '@models/contact-payload.model.js' -const execPrm = promisify(exec) - ;(async () => { const alfredClient = new FastAlfred() @@ -18,5 +15,5 @@ const execPrm = promisify(exec) const urlNew = `whatsapp://send?phone=${number}` - await execPrm(`open ${urlNew}`) + await execPromise(`open ${urlNew}`) })() diff --git a/src/models/contact-payload.model.ts b/src/models/contact-payload.model.ts index 64c8622..6770da3 100644 --- a/src/models/contact-payload.model.ts +++ b/src/models/contact-payload.model.ts @@ -1,6 +1,8 @@ import type { CountryCode } from 'libphonenumber-js' +import type { SupportedPlatform } from './platform.model.js' export interface ContactPayload { phoneNumber: string countryCode: CountryCode + platform: SupportedPlatform } diff --git a/src/models/platform.model.ts b/src/models/platform.model.ts new file mode 100644 index 0000000..2a2d187 --- /dev/null +++ b/src/models/platform.model.ts @@ -0,0 +1,2 @@ +export const SUPPORTED_PLATFORMS = ['whatsapp', 'sms', 'call'] as const +export type SupportedPlatform = (typeof SUPPORTED_PLATFORMS)[number]