Skip to content

Commit

Permalink
Add countries Pack
Browse files Browse the repository at this point in the history
  • Loading branch information
erickoledadevrel committed Aug 21, 2024
1 parent d9c3bc6 commit 8d6ee9f
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
3 changes: 3 additions & 0 deletions countries/.coda-pack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"packId": 33508
}
122 changes: 122 additions & 0 deletions countries/pack.ts
Original file line number Diff line number Diff line change
@@ -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),
};
}
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 8d6ee9f

Please sign in to comment.