From e18d40a8997cb19b248f6de8b70303603faec9a6 Mon Sep 17 00:00:00 2001 From: Matteo Rezzin Date: Fri, 15 Nov 2024 16:13:21 +0100 Subject: [PATCH] first commit --- .editorconfig | 12 + .github/workflows/ci.yml | 45 ++ .gitignore | 56 +++ .npmrc | 2 + .vscode/settings.json | 3 + README.md | 84 ++++ eslint.config.mjs | 20 + package.json | 47 ++ playground/app.vue | 14 + playground/nuxt.config.ts | 5 + playground/package.json | 13 + playground/server/tsconfig.json | 3 + playground/tsconfig.json | 3 + src/module.ts | 27 ++ src/runtime/composables/useUserCountry.ts | 527 ++++++++++++++++++++++ src/runtime/server/tsconfig.json | 3 + test/basic.test.ts | 15 + test/fixtures/basic/app.vue | 6 + test/fixtures/basic/nuxt.config.ts | 7 + test/fixtures/basic/package.json | 5 + tsconfig.json | 8 + 21 files changed, 905 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 .npmrc create mode 100644 .vscode/settings.json create mode 100644 README.md create mode 100644 eslint.config.mjs create mode 100644 package.json create mode 100644 playground/app.vue create mode 100644 playground/nuxt.config.ts create mode 100644 playground/package.json create mode 100644 playground/server/tsconfig.json create mode 100644 playground/tsconfig.json create mode 100644 src/module.ts create mode 100644 src/runtime/composables/useUserCountry.ts create mode 100644 src/runtime/server/tsconfig.json create mode 100644 test/basic.test.ts create mode 100644 test/fixtures/basic/app.vue create mode 100644 test/fixtures/basic/nuxt.config.ts create mode 100644 test/fixtures/basic/package.json create mode 100644 tsconfig.json diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..007463b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +indent_size = 2 +indent_style = space +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..c44c8e0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +name: ci + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + lint: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npx nypm@latest i + + - name: Lint + run: npm run lint + + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - run: corepack enable + - uses: actions/setup-node@v4 + with: + node-version: 20 + + - name: Install dependencies + run: npx nypm@latest i + + - name: Playground prepare + run: npm run dev:prepare + + - name: Test + run: npm run test diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a3eb22 --- /dev/null +++ b/.gitignore @@ -0,0 +1,56 @@ +# Dependencies +node_modules + +# Logs +*.log* + +# Temp directories +.temp +.tmp +.cache + +# Yarn +**/.yarn/cache +**/.yarn/*state* + +# Generated dirs +dist + +# Nuxt +.nuxt +.output +.data +.vercel_build_output +.build-* +.netlify + +# Env +.env + +# Testing +reports +coverage +*.lcov +.nyc_output + +# VSCode +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Intellij idea +*.iml +.idea + +# OSX +.DS_Store +.AppleDouble +.LSOverride +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..cf04042 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +shamefully-hoist=true +strict-peer-dependencies=false diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..eb8204b --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "eslint.experimental.useFlatConfig": true +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..b168aa1 --- /dev/null +++ b/README.md @@ -0,0 +1,84 @@ + + +# My Module + +[![npm version][npm-version-src]][npm-version-href] +[![npm downloads][npm-downloads-src]][npm-downloads-href] +[![License][license-src]][license-href] +[![Nuxt][nuxt-src]][nuxt-href] + +My new Nuxt module for doing amazing things. + +- [✨  Release Notes](/CHANGELOG.md) + + + +## Features + + +- ⛰  Foo +- 🚠  Bar +- 🌲  Baz + +## Quick Setup + +Install the module to your Nuxt application with one command: + +```bash +npx nuxi module add my-module +``` + +That's it! You can now use My Module in your Nuxt app ✨ + + +## Contribution + +
+ Local development + + ```bash + # Install dependencies + npm install + + # Generate type stubs + npm run dev:prepare + + # Develop with the playground + npm run dev + + # Build the playground + npm run dev:build + + # Run ESLint + npm run lint + + # Run Vitest + npm run test + npm run test:watch + + # Release new version + npm run release + ``` + +
+ + + +[npm-version-src]: https://img.shields.io/npm/v/my-module/latest.svg?style=flat&colorA=020420&colorB=00DC82 +[npm-version-href]: https://npmjs.com/package/my-module + +[npm-downloads-src]: https://img.shields.io/npm/dm/my-module.svg?style=flat&colorA=020420&colorB=00DC82 +[npm-downloads-href]: https://npm.chart.dev/my-module + +[license-src]: https://img.shields.io/npm/l/my-module.svg?style=flat&colorA=020420&colorB=00DC82 +[license-href]: https://npmjs.com/package/my-module + +[nuxt-src]: https://img.shields.io/badge/Nuxt-020420?logo=nuxt.js +[nuxt-href]: https://nuxt.com diff --git a/eslint.config.mjs b/eslint.config.mjs new file mode 100644 index 0000000..27b2e71 --- /dev/null +++ b/eslint.config.mjs @@ -0,0 +1,20 @@ +// @ts-check +import { createConfigForNuxt } from '@nuxt/eslint-config/flat' + +// Run `npx @eslint/config-inspector` to inspect the resolved config interactively +export default createConfigForNuxt({ + features: { + // Rules for module authors + tooling: true, + // Rules for formatting + stylistic: true, + }, + dirs: { + src: [ + './playground', + ], + }, +}) + .append( + // your custom flat config here... + ) diff --git a/package.json b/package.json new file mode 100644 index 0000000..05a3787 --- /dev/null +++ b/package.json @@ -0,0 +1,47 @@ +{ + "name": "my-module", + "version": "1.0.0", + "description": "My new Nuxt module", + "repository": "your-org/my-module", + "license": "MIT", + "type": "module", + "exports": { + ".": { + "types": "./dist/types.d.ts", + "import": "./dist/module.mjs", + "require": "./dist/module.cjs" + } + }, + "main": "./dist/module.cjs", + "types": "./dist/types.d.ts", + "files": [ + "dist" + ], + "scripts": { + "prepack": "nuxt-module-build build", + "dev": "nuxi dev playground", + "dev:build": "nuxi build playground", + "dev:prepare": "nuxt-module-build build --stub && nuxt-module-build prepare && nuxi prepare playground", + "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags", + "lint": "eslint .", + "test": "vitest run", + "test:watch": "vitest watch", + "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit" + }, + "dependencies": { + "@nuxt/kit": "^3.14.159" }, + "devDependencies": { + "@nuxt/devtools": "^1.6.0", + "@nuxt/eslint-config": "^0.6.1", + "@nuxt/module-builder": "^0.8.4", + "@nuxt/schema": "^3.14.159", + "@nuxt/test-utils": "^3.14.4", + "@types/node": "latest", + "changelogen": "^0.5.7", + "eslint": "^9.14.0", + "nuxt": "^3.14.159", + "typescript": "latest", + "vitest": "^2.1.5", + "vue-tsc": "^2.1.10" + } +} diff --git a/playground/app.vue b/playground/app.vue new file mode 100644 index 0000000..5b79c37 --- /dev/null +++ b/playground/app.vue @@ -0,0 +1,14 @@ + + + diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts new file mode 100644 index 0000000..e53b475 --- /dev/null +++ b/playground/nuxt.config.ts @@ -0,0 +1,5 @@ +export default defineNuxtConfig({ + modules: ['../src/module'], + devtools: { enabled: true }, + compatibilityDate: '2024-11-15', +}) \ No newline at end of file diff --git a/playground/package.json b/playground/package.json new file mode 100644 index 0000000..e6e9ac8 --- /dev/null +++ b/playground/package.json @@ -0,0 +1,13 @@ +{ + "private": true, + "name": "my-module-playground", + "type": "module", + "scripts": { + "dev": "nuxi dev", + "build": "nuxi build", + "generate": "nuxi generate" + }, + "dependencies": { + "nuxt": "^3.14.159" + } +} diff --git a/playground/server/tsconfig.json b/playground/server/tsconfig.json new file mode 100644 index 0000000..b9ed69c --- /dev/null +++ b/playground/server/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../.nuxt/tsconfig.server.json" +} diff --git a/playground/tsconfig.json b/playground/tsconfig.json new file mode 100644 index 0000000..4b34df1 --- /dev/null +++ b/playground/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "./.nuxt/tsconfig.json" +} diff --git a/src/module.ts b/src/module.ts new file mode 100644 index 0000000..415219d --- /dev/null +++ b/src/module.ts @@ -0,0 +1,27 @@ +import { defineNuxtModule, createResolver, addImportsDir } from "@nuxt/kit"; +import { defu } from "defu"; + +export interface ModuleOptions { + endpoint: string; +} + +export default defineNuxtModule({ + meta: { + name: "User Country", + configKey: "userCountry", + }, + defaults: { + endpoint: "https://where-am-i.jamflow.app/", + }, + setup(_options, _nuxt) { + const resolver = createResolver(import.meta.url); + _nuxt.options.runtimeConfig.public.userCountry = defu( + _nuxt.options.runtimeConfig.public.userCountry, + { + endpoint: _options.endpoint, + } + ); + + addImportsDir(resolver.resolve("./runtime/composables")); + }, +}); diff --git a/src/runtime/composables/useUserCountry.ts b/src/runtime/composables/useUserCountry.ts new file mode 100644 index 0000000..2304a59 --- /dev/null +++ b/src/runtime/composables/useUserCountry.ts @@ -0,0 +1,527 @@ +import { createError, useFetch, useRuntimeConfig } from "#app"; +type CountryEnum = + | "AF" + | "AX" + | "AL" + | "DZ" + | "AS" + | "AD" + | "AO" + | "AI" + | "AQ" + | "AG" + | "AR" + | "AM" + | "AW" + | "AU" + | "AT" + | "AZ" + | "BS" + | "BH" + | "BD" + | "BB" + | "BY" + | "BE" + | "BZ" + | "BJ" + | "BM" + | "BT" + | "BO" + | "BQ" + | "BA" + | "BW" + | "BV" + | "BR" + | "IO" + | "BN" + | "BG" + | "BF" + | "BI" + | "CV" + | "KH" + | "CM" + | "CA" + | "KY" + | "CF" + | "TD" + | "CL" + | "CN" + | "CX" + | "CC" + | "CO" + | "KM" + | "CD" + | "CG" + | "CK" + | "CR" + | "CI" + | "HR" + | "CU" + | "CW" + | "CY" + | "CZ" + | "DK" + | "DJ" + | "DM" + | "DO" + | "EC" + | "EG" + | "SV" + | "GQ" + | "ER" + | "EE" + | "ET" + | "FK" + | "FO" + | "FJ" + | "FI" + | "FR" + | "GF" + | "PF" + | "TF" + | "GA" + | "GM" + | "GE" + | "DE" + | "GH" + | "GI" + | "GR" + | "GL" + | "GD" + | "GP" + | "GU" + | "GT" + | "GG" + | "GN" + | "GW" + | "GY" + | "HT" + | "HM" + | "VA" + | "HN" + | "HK" + | "HU" + | "IS" + | "IN" + | "ID" + | "IR" + | "IQ" + | "IE" + | "IM" + | "IL" + | "IT" + | "JM" + | "JP" + | "JE" + | "JO" + | "KZ" + | "KE" + | "KI" + | "KP" + | "KR" + | "KW" + | "KG" + | "LA" + | "LV" + | "LB" + | "LS" + | "LR" + | "LY" + | "LI" + | "LT" + | "LU" + | "MO" + | "MK" + | "MG" + | "MW" + | "MY" + | "MV" + | "ML" + | "MT" + | "MH" + | "MQ" + | "MR" + | "MU" + | "YT" + | "MX" + | "FM" + | "MD" + | "MC" + | "MN" + | "ME" + | "MS" + | "MA" + | "MZ" + | "MM" + | "NA" + | "NR" + | "NP" + | "NL" + | "NC" + | "NZ" + | "NI" + | "NE" + | "NG" + | "NU" + | "NF" + | "MP" + | "NO" + | "OM" + | "PK" + | "PW" + | "PS" + | "PA" + | "PG" + | "PY" + | "PE" + | "PH" + | "PN" + | "PL" + | "PT" + | "PR" + | "QA" + | "RE" + | "RO" + | "RU" + | "RW" + | "BL" + | "SH" + | "KN" + | "LC" + | "MF" + | "PM" + | "VC" + | "WS" + | "SM" + | "ST" + | "SA" + | "SN" + | "RS" + | "SC" + | "SL" + | "SG" + | "SX" + | "SK" + | "SI" + | "SB" + | "SO" + | "ZA" + | "GS" + | "SS" + | "ES" + | "LK" + | "SD" + | "SR" + | "SJ" + | "SZ" + | "SE" + | "CH" + | "SY" + | "TW" + | "TJ" + | "TZ" + | "TH" + | "TL" + | "TG" + | "TK" + | "TO" + | "TT" + | "TN" + | "TR" + | "TM" + | "TC" + | "TV" + | "UG" + | "UA" + | "AE" + | "GB" + | "UM" + | "US" + | "UY" + | "UZ" + | "VU" + | "VE" + | "VN" + | "VG" + | "VI" + | "WF" + | "EH" + | "YE" + | "ZM" + | "ZW"; + +interface Country { + name: string; + iso: CountryEnum; +} + +const countrys: Country[] = [ + { name: "Afghanistan", iso: "AF" }, + { name: "Åland Islands", iso: "AX" }, + { name: "Albania", iso: "AL" }, + { name: "Algeria", iso: "DZ" }, + { name: "American Samoa", iso: "AS" }, + { name: "Andorra", iso: "AD" }, + { name: "Angola", iso: "AO" }, + { name: "Anguilla", iso: "AI" }, + { name: "Antarctica", iso: "AQ" }, + { name: "Antigua and Barbuda", iso: "AG" }, + { name: "Argentina", iso: "AR" }, + { name: "Armenia", iso: "AM" }, + { name: "Aruba", iso: "AW" }, + { name: "Australia", iso: "AU" }, + { name: "Austria", iso: "AT" }, + { name: "Azerbaijan", iso: "AZ" }, + { name: "Bahamas, The", iso: "BS" }, + { name: "Bahrain", iso: "BH" }, + { name: "Bangladesh", iso: "BD" }, + { name: "Barbados", iso: "BB" }, + { name: "Belarus", iso: "BY" }, + { name: "Belgium", iso: "BE" }, + { name: "Belize", iso: "BZ" }, + { name: "Benin", iso: "BJ" }, + { name: "Bermuda", iso: "BM" }, + { name: "Bhutan", iso: "BT" }, + { name: "Bolivia", iso: "BO" }, + { name: "Bonaire, Sint Eustatius and Saba", iso: "BQ" }, + { name: "Bosnia and Herzegovina", iso: "BA" }, + { name: "Botswana", iso: "BW" }, + { name: "Bouvet Island", iso: "BV" }, + { name: "Brazil", iso: "BR" }, + { name: "British Indian Ocean Territory", iso: "IO" }, + { name: "Brunei", iso: "BN" }, + { name: "Bulgaria", iso: "BG" }, + { name: "Burkina Faso", iso: "BF" }, + { name: "Burundi", iso: "BI" }, + { name: "Cape Verde", iso: "CV" }, + { name: "Cambodia", iso: "KH" }, + { name: "Cameroon", iso: "CM" }, + { name: "Canada", iso: "CA" }, + { name: "Cayman Islands", iso: "KY" }, + { name: "Central African Republic", iso: "CF" }, + { name: "Chad", iso: "TD" }, + { name: "Chile", iso: "CL" }, + { name: "China", iso: "CN" }, + { name: "Christmas Island", iso: "CX" }, + { name: "Cocos (Keeling) Islands", iso: "CC" }, + { name: "Colombia", iso: "CO" }, + { name: "Comoros", iso: "KM" }, + { name: "Congo (DRC)", iso: "CD" }, + { name: "Congo", iso: "CG" }, + { name: "Cook Islands", iso: "CK" }, + { name: "Costa Rica", iso: "CR" }, + { name: "Côte d'Ivoire", iso: "CI" }, + { name: "Croatia", iso: "HR" }, + { name: "Cuba", iso: "CU" }, + { name: "Curaçao", iso: "CW" }, + { name: "Cyprus", iso: "CY" }, + { name: "Czech Republic", iso: "CZ" }, + { name: "Denmark", iso: "DK" }, + { name: "Djibouti", iso: "DJ" }, + { name: "Dominica", iso: "DM" }, + { name: "Dominican Republic", iso: "DO" }, + { name: "Ecuador", iso: "EC" }, + { name: "Egypt", iso: "EG" }, + { name: "El Salvador", iso: "SV" }, + { name: "Equatorial Guinea", iso: "GQ" }, + { name: "Eritrea", iso: "ER" }, + { name: "Estonia", iso: "EE" }, + { name: "Ethiopia", iso: "ET" }, + { name: "Falkland Islands (Islas Malvinas)", iso: "FK" }, + { name: "Faroe Islands", iso: "FO" }, + { name: "Fiji Islands", iso: "FJ" }, + { name: "Finland", iso: "FI" }, + { name: "France", iso: "FR" }, + { name: "French Guiana", iso: "GF" }, + { name: "French Polynesia", iso: "PF" }, + { name: "French Southern and Antarctic Lands", iso: "TF" }, + { name: "Gabon", iso: "GA" }, + { name: "Gambia, The", iso: "GM" }, + { name: "Georgia", iso: "GE" }, + { name: "Germany", iso: "DE" }, + { name: "Ghana", iso: "GH" }, + { name: "Gibraltar", iso: "GI" }, + { name: "Greece", iso: "GR" }, + { name: "Greenland", iso: "GL" }, + { name: "Grenada", iso: "GD" }, + { name: "Guadeloupe", iso: "GP" }, + { name: "Guam", iso: "GU" }, + { name: "Guatemala", iso: "GT" }, + { name: "Guernsey", iso: "GG" }, + { name: "Guinea", iso: "GN" }, + { name: "Guinea-Bissau", iso: "GW" }, + { name: "Guyana", iso: "GY" }, + { name: "Haiti", iso: "HT" }, + { name: "Heard Island and McDonald Islands", iso: "HM" }, + { name: "Vatican City", iso: "VA" }, + { name: "Honduras", iso: "HN" }, + { name: "Hong Kong SAR", iso: "HK" }, + { name: "Hungary", iso: "HU" }, + { name: "Iceland", iso: "IS" }, + { name: "India", iso: "IN" }, + { name: "Indonesia", iso: "ID" }, + { name: "Iran", iso: "IR" }, + { name: "Iraq", iso: "IQ" }, + { name: "Ireland", iso: "IE" }, + { name: "Man, Isle of", iso: "IM" }, + { name: "Israel", iso: "IL" }, + { name: "Italy", iso: "IT" }, + { name: "Jamaica", iso: "JM" }, + { name: "Japan", iso: "JP" }, + { name: "Jersey", iso: "JE" }, + { name: "Jordan", iso: "JO" }, + { name: "Kazakhstan", iso: "KZ" }, + { name: "Kenya", iso: "KE" }, + { name: "Kiribati", iso: "KI" }, + { name: "North Korea", iso: "KP" }, + { name: "Korea", iso: "KR" }, + { name: "Kuwait", iso: "KW" }, + { name: "Kyrgyzstan", iso: "KG" }, + { name: "Laos", iso: "LA" }, + { name: "Latvia", iso: "LV" }, + { name: "Lebanon", iso: "LB" }, + { name: "Lesotho", iso: "LS" }, + { name: "Liberia", iso: "LR" }, + { name: "Libya", iso: "LY" }, + { name: "Liechtenstein", iso: "LI" }, + { name: "Lithuania", iso: "LT" }, + { name: "Luxembourg", iso: "LU" }, + { name: "Macao SAR", iso: "MO" }, + { name: "Macedonia, Former Yugoslav Republic of", iso: "MK" }, + { name: "Madagascar", iso: "MG" }, + { name: "Malawi", iso: "MW" }, + { name: "Malaysia", iso: "MY" }, + { name: "Maldives", iso: "MV" }, + { name: "Mali", iso: "ML" }, + { name: "Malta", iso: "MT" }, + { name: "Marshall Islands", iso: "MH" }, + { name: "Martinique", iso: "MQ" }, + { name: "Mauritania", iso: "MR" }, + { name: "Mauritius", iso: "MU" }, + { name: "Mayotte", iso: "YT" }, + { name: "Mexico", iso: "MX" }, + { name: "Micronesia", iso: "FM" }, + { name: "Moldova", iso: "MD" }, + { name: "Monaco", iso: "MC" }, + { name: "Mongolia", iso: "MN" }, + { name: "Montenegro", iso: "ME" }, + { name: "Montserrat", iso: "MS" }, + { name: "Morocco", iso: "MA" }, + { name: "Mozambique", iso: "MZ" }, + { name: "Myanmar", iso: "MM" }, + { name: "Namibia", iso: "NA" }, + { name: "Nauru", iso: "NR" }, + { name: "Nepal", iso: "NP" }, + { name: "Netherlands", iso: "NL" }, + { name: "New Caledonia", iso: "NC" }, + { name: "New Zealand", iso: "NZ" }, + { name: "Nicaragua", iso: "NI" }, + { name: "Niger", iso: "NE" }, + { name: "Nigeria", iso: "NG" }, + { name: "Niue", iso: "NU" }, + { name: "Norfolk Island", iso: "NF" }, + { name: "Northern Mariana Islands", iso: "MP" }, + { name: "Norway", iso: "NO" }, + { name: "Oman", iso: "OM" }, + { name: "Pakistan", iso: "PK" }, + { name: "Palau", iso: "PW" }, + { name: "Palestinian Authority", iso: "PS" }, + { name: "Panama", iso: "PA" }, + { name: "Papua New Guinea", iso: "PG" }, + { name: "Paraguay", iso: "PY" }, + { name: "Peru", iso: "PE" }, + { name: "Philippines", iso: "PH" }, + { name: "Pitcairn Islands", iso: "PN" }, + { name: "Poland", iso: "PL" }, + { name: "Portugal", iso: "PT" }, + { name: "Puerto Rico", iso: "PR" }, + { name: "Qatar", iso: "QA" }, + { name: "Réunion", iso: "RE" }, + { name: "Romania", iso: "RO" }, + { name: "Russia", iso: "RU" }, + { name: "Rwanda", iso: "RW" }, + { name: "Saint Barthélemy", iso: "BL" }, + { name: "Saint Helena, Ascension and Tristan da Cunha", iso: "SH" }, + { name: "St. Kitts and Nevis", iso: "KN" }, + { name: "St. Lucia", iso: "LC" }, + { name: "Saint Martin (French part)", iso: "MF" }, + { name: "St. Pierre and Miquelon", iso: "PM" }, + { name: "St. Vincent and the Grenadines", iso: "VC" }, + { name: "Samoa", iso: "WS" }, + { name: "San Marino", iso: "SM" }, + { name: "São Tomé and Príncipe", iso: "ST" }, + { name: "Saudi Arabia", iso: "SA" }, + { name: "Senegal", iso: "SN" }, + { name: "Serbia", iso: "RS" }, + { name: "Seychelles", iso: "SC" }, + { name: "Sierra Leone", iso: "SL" }, + { name: "Singapore", iso: "SG" }, + { name: "Sint Maarten (Dutch part)", iso: "SX" }, + { name: "Slovakia", iso: "SK" }, + { name: "Slovenia", iso: "SI" }, + { name: "Solomon Islands", iso: "SB" }, + { name: "Somalia", iso: "SO" }, + { name: "South Africa", iso: "ZA" }, + { name: "South Georgia and the South Sandwich Islands", iso: "GS" }, + { name: "South Sudan", iso: "SS" }, + { name: "Spain", iso: "ES" }, + { name: "Sri Lanka", iso: "LK" }, + { name: "Sudan", iso: "SD" }, + { name: "Suriname", iso: "SR" }, + { name: "Svalbard", iso: "SJ" }, + { name: "Swaziland", iso: "SZ" }, + { name: "Sweden", iso: "SE" }, + { name: "Switzerland", iso: "CH" }, + { name: "Syria", iso: "SY" }, + { name: "Taiwan", iso: "TW" }, + { name: "Tajikistan", iso: "TJ" }, + { name: "Tanzania", iso: "TZ" }, + { name: "Thailand", iso: "TH" }, + { name: "Democratic Republic of Timor-Leste", iso: "TL" }, + { name: "Togo", iso: "TG" }, + { name: "Tokelau", iso: "TK" }, + { name: "Tonga", iso: "TO" }, + { name: "Trinidad and Tobago", iso: "TT" }, + { name: "Tunisia", iso: "TN" }, + { name: "Turkey", iso: "TR" }, + { name: "Turkmenistan", iso: "TM" }, + { name: "Turks and Caicos Islands", iso: "TC" }, + { name: "Tuvalu", iso: "TV" }, + { name: "Uganda", iso: "UG" }, + { name: "Ukraine", iso: "UA" }, + { name: "United Arab Emirates", iso: "AE" }, + { name: "United Kingdom", iso: "GB" }, + { name: "U.S. Minor Outlying Islands", iso: "UM" }, + { name: "United States", iso: "US" }, + { name: "Uruguay", iso: "UY" }, + { name: "Uzbekistan", iso: "UZ" }, + { name: "Vanuatu", iso: "VU" }, + { name: "Bolivarian Republic of Venezuela", iso: "VE" }, + { name: "Vietnam", iso: "VN" }, + { name: "Virgin Islands, British", iso: "VG" }, + { name: "Virgin Islands", iso: "VI" }, + { name: "Wallis and Futuna", iso: "WF" }, + { name: "Western Sahara", iso: "EH" }, + { name: "Yemen", iso: "YE" }, + { name: "Zambia", iso: "ZM" }, + { name: "Zimbabwe", iso: "ZW" }, +]; + +const countryMap = new Map( + countrys.map((country) => [country.iso, country]) +); + +export function useUserCountry() { + const endPointUrl = useRuntimeConfig().public.userCountry.endpoint; + + return useFetch(endPointUrl, { + server: false, + transform: (data) => { + const country = countryMap.get(data as CountryEnum); + if (!country) { + throw createError("Country not found"); + } + return country; + }, + }); +} diff --git a/src/runtime/server/tsconfig.json b/src/runtime/server/tsconfig.json new file mode 100644 index 0000000..e95a776 --- /dev/null +++ b/src/runtime/server/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../../.nuxt/tsconfig.server.json", +} diff --git a/test/basic.test.ts b/test/basic.test.ts new file mode 100644 index 0000000..5167cad --- /dev/null +++ b/test/basic.test.ts @@ -0,0 +1,15 @@ +import { fileURLToPath } from 'node:url' +import { describe, it, expect } from 'vitest' +import { setup, $fetch } from '@nuxt/test-utils/e2e' + +describe('ssr', async () => { + await setup({ + rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)), + }) + + it('renders the index page', async () => { + // Get response to a server-rendered page with `$fetch`. + const html = await $fetch('/') + expect(html).toContain('
basic
') + }) +}) diff --git a/test/fixtures/basic/app.vue b/test/fixtures/basic/app.vue new file mode 100644 index 0000000..29a9c81 --- /dev/null +++ b/test/fixtures/basic/app.vue @@ -0,0 +1,6 @@ + + + diff --git a/test/fixtures/basic/nuxt.config.ts b/test/fixtures/basic/nuxt.config.ts new file mode 100644 index 0000000..ed0c53f --- /dev/null +++ b/test/fixtures/basic/nuxt.config.ts @@ -0,0 +1,7 @@ +import MyModule from '../../../src/module' + +export default defineNuxtConfig({ + modules: [ + MyModule, + ], +}) diff --git a/test/fixtures/basic/package.json b/test/fixtures/basic/package.json new file mode 100644 index 0000000..decd433 --- /dev/null +++ b/test/fixtures/basic/package.json @@ -0,0 +1,5 @@ +{ + "private": true, + "name": "basic", + "type": "module" +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..59ad4a8 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "./.nuxt/tsconfig.json", + "exclude": [ + "dist", + "node_modules", + "playground", + ] +}