diff --git a/countries/.coda-pack.json b/countries/.coda-pack.json new file mode 100644 index 0000000..a9f6925 --- /dev/null +++ b/countries/.coda-pack.json @@ -0,0 +1,3 @@ +{ + "packId": 33508 +} \ No newline at end of file diff --git a/countries/pack.ts b/countries/pack.ts new file mode 100644 index 0000000..934dc9e --- /dev/null +++ b/countries/pack.ts @@ -0,0 +1,122 @@ +import * as coda from "@codahq/packs-sdk"; +import { countries, languages, continents, getEmojiFlag, TCountryCode } from "countries-list"; + +export const pack = coda.newPack(); + +const ContinentSchema = coda.makeObjectSchema({ + properties: { + name: { type: coda.ValueType.String }, + code: { type: coda.ValueType.String }, + }, + displayProperty: "name", +}); + +const LanguageSchema = coda.makeObjectSchema({ + properties: { + name: { type: coda.ValueType.String }, + code: { type: coda.ValueType.String, description: "ISO 639-1" }, + localized: { type: coda.ValueType.String, fromKey: "native" }, + }, + displayProperty: "name", +}); + +const CountrySchema = coda.makeObjectSchema({ + properties: { + name: { type: coda.ValueType.String }, + code: { type: coda.ValueType.String, description: "ISO 3166-1 alpha-2" }, + localized: { type: coda.ValueType.String, fromKey: "native" }, + continent: ContinentSchema, + capital: { type: coda.ValueType.String }, + languages: { + type: coda.ValueType.Array, + items: LanguageSchema, + }, + callingCodes: { + type: coda.ValueType.Array, + items: { type: coda.ValueType.Number }, + fromKey: "phone", + }, + currencyCodes: { + type: coda.ValueType.Array, + items: { type: coda.ValueType.String }, + fromKey: "currency", + description: "ISO 4217", + }, + flagEmoji: { + type: coda.ValueType.String, + description: "Flag emojis may not be rendered correctly on some devices." + }, + }, + displayProperty: "name", + idProperty: "code", + featuredProperties: ["code", "languages"], +}); + +pack.addSyncTable({ + name: "Countries", + description: "Lists all of countries (as defined by ISO 3166).", + identityName: "Country", + schema: CountrySchema, + formula: { + name: "SyncCountries", + description: "Syncs the data.", + parameters: [], + execute: async function (args, context) { + let rows = Object.keys(countries).map(code => { + return getCountry(code); + }); + return { + result: rows, + }; + }, + }, +}); + +pack.addFormula({ + name: "Country", + description: "Get country information from a country code.", + parameters: [ + coda.makeParameter({ + type: coda.ParameterType.String, + name: "code", + description: "The ISO 3166-1 alpha-2 country code.", + }), + ], + resultType: coda.ValueType.Object, + schema: CountrySchema, + examples: [ + { params: ["US"], result: getCountry("US") }, + ], + execute: async function (args, context) { + let [code] = args; + return getCountry(code); + }, +}); + +pack.addColumnFormat({ + name: "Country", + instructions: "Enter a two letter country code.", + formulaName: "Country", +}); + +function getCountry(code: string) { + let country = countries[code]; + if (!country) { + throw new coda.UserVisibleError(`Unknown country code: ${code}`); + } + return { + code, + ...country, + continent: { + code: country.continent, + name: continents[country.continent], + }, + languages: country.languages.map(lang => { + return { + code: lang, + ...languages[lang], + }; + }), + flagEmoji: getEmojiFlag(code as TCountryCode), + }; +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 1d1c173..8215841 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,6 +17,7 @@ "cheerio": "^1.0.0-rc.12", "color-convert": "^2.0.1", "content-disposition": "^0.5.4", + "countries-list": "^3.1.1", "css-named-colors": "^1.0.1", "date-holidays": "^3.23.8", "diff": "^5.1.0", @@ -4266,6 +4267,11 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, + "node_modules/countries-list": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/countries-list/-/countries-list-3.1.1.tgz", + "integrity": "sha512-nPklKJ5qtmY5MdBKw1NiBAoyx5Sa7p2yPpljZyQ7gyCN1m+eMFs9I6CT37Mxt8zvR5L3VzD3DJBE4WQzX3WF4A==" + }, "node_modules/create-ecdh": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.4.tgz", @@ -20555,6 +20561,11 @@ "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz", "integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ==" }, + "countries-list": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/countries-list/-/countries-list-3.1.1.tgz", + "integrity": "sha512-nPklKJ5qtmY5MdBKw1NiBAoyx5Sa7p2yPpljZyQ7gyCN1m+eMFs9I6CT37Mxt8zvR5L3VzD3DJBE4WQzX3WF4A==" + }, "create-ecdh": { "version": "4.0.4", "resolved": "https://registry.npmjs.org/create-ecdh/-/create-ecdh-4.0.4.tgz", diff --git a/package.json b/package.json index d236f13..347277b 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "cheerio": "^1.0.0-rc.12", "color-convert": "^2.0.1", "content-disposition": "^0.5.4", + "countries-list": "^3.1.1", "css-named-colors": "^1.0.1", "date-holidays": "^3.23.8", "diff": "^5.1.0",