|
1 | 1 | # Development
|
| 2 | + |
| 3 | +## Adding services |
| 4 | + |
| 5 | +Let's analyze the creation of a new `Bitcoin` service, which will receive information from an external API. |
| 6 | + |
| 7 | +#### 1. Create interface |
| 8 | + |
| 9 | +The first step is to designate the service interaction interface. It is necessary to define additional properties: |
| 10 | + |
| 11 | +::: code-group |
| 12 | +```typescript [types/services.d.ts] |
| 13 | +export interface BitcoinService extends BaseService { |
| 14 | + options?: { |
| 15 | + code: string |
| 16 | + interval?: number |
| 17 | + } |
| 18 | +} |
| 19 | +``` |
| 20 | +::: |
| 21 | + |
| 22 | +#### 2. Create component |
| 23 | + |
| 24 | +::: code-group |
| 25 | +```vue [components/service/bitcoin.vue] |
| 26 | +<template> |
| 27 | + <ServiceBase v-bind="props"> |
| 28 | + <template #title> |
| 29 | + {{ $('service.bitcoin.title', { code: options.code }) }} |
| 30 | + </template> |
| 31 | + <template #description> |
| 32 | + {{ $('service.bitcoin.description', { rate: data?.rate || '0' }) }} |
| 33 | + </template> |
| 34 | + </ServiceBase> |
| 35 | +</template> |
| 36 | +
|
| 37 | +<script setup lang="ts"> |
| 38 | +import type { BitcoinService } from '~/types' |
| 39 | +
|
| 40 | +const props = defineProps<BitcoinService>() |
| 41 | +const { data, pauseUpdate } = useServiceData<BitcoinService, { rate: string }>(props, { |
| 42 | + updateInterval: props?.options?.interval |
| 43 | +}) |
| 44 | +
|
| 45 | +onBeforeUnmount(pauseUpdate) |
| 46 | +</script> |
| 47 | +``` |
| 48 | +::: |
| 49 | + |
| 50 | +#### 3. Writing translations |
| 51 | + |
| 52 | +If static text is used in the component, it should be placed in a translation file. |
| 53 | +It is required to add `en-US.json` translations, the rest are optional. |
| 54 | + |
| 55 | +::: code-group |
| 56 | +```json [locales/ru-RU.json] |
| 57 | +{ |
| 58 | + "service": { |
| 59 | + "bitcoin": { // [!code focus] |
| 60 | + "title": "Code {code}", // [!code focus] |
| 61 | + "description": "Rate {rate}" // [!code focus] |
| 62 | + } // [!code focus] |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | +::: |
| 67 | + |
| 68 | +Translations are based on [vue-i18n](https://vue-i18n.intlify.dev/). Absolutely all features are available. |
| 69 | + |
| 70 | +#### 4. Data retrieval |
| 71 | + |
| 72 | +::: code-group |
| 73 | +```typescript [server/api/services/bitcoin.ts] |
| 74 | +import type { BitcoinService } from '~/types' |
| 75 | + |
| 76 | +export default defineEventHandler(async (event): Promise<{ rate: string }> => { |
| 77 | + const service = await getService<BitcoinService>(event) |
| 78 | + |
| 79 | + try { |
| 80 | + const data = await $fetch('https://api.coindesk.com/v1/bpi/currentprice.json', { |
| 81 | + parseResponse: (text) => JSON.parse(text) |
| 82 | + }) |
| 83 | + |
| 84 | + return { |
| 85 | + rate: data.bpi[service.option.code].rate |
| 86 | + } |
| 87 | + } catch (e) { |
| 88 | + logger.error(e) |
| 89 | + } |
| 90 | + |
| 91 | + return { |
| 92 | + rate: '-' |
| 93 | + } |
| 94 | +}) |
| 95 | +``` |
| 96 | +::: |
| 97 | + |
| 98 | +#### 5. Updating documentation |
| 99 | + |
| 100 | +Don't forget to add the service to the documentation so that other users are aware and can use it. |
| 101 | + |
| 102 | +To do this, create files: |
| 103 | + |
| 104 | +* `docs/services/bitcoin.md` |
| 105 | +* `docs/{lang}/services/bitcoin.md` - can be left empty |
| 106 | + |
| 107 | +## Creating theme |
| 108 | + |
| 109 | +#### 1. Adding CSS variables |
| 110 | + |
| 111 | +::: code-group |
| 112 | +```css [assets/style/tailwind.css] |
| 113 | +html.new-theme { |
| 114 | + --fg: 67 52 34; |
| 115 | + --fg-dimmed: 80 66 49; |
| 116 | + --background: 241 231 208; |
| 117 | +} |
| 118 | +``` |
| 119 | +::: |
| 120 | + |
| 121 | +#### 2. Adding to Config |
| 122 | + |
| 123 | +::: code-group |
| 124 | +```typescript [types/config.d.ts] |
| 125 | +export interface Config { |
| 126 | + title?: string |
| 127 | + lang: 'en' | 'ru' |
| 128 | + theme?: 'system' | 'light' | 'dark' | 'deep' | 'new-theme' // [!code focus] |
| 129 | + services: ServicesGroup[] |
| 130 | + checkUpdates: boolean |
| 131 | +} |
| 132 | +``` |
| 133 | +::: |
| 134 | + |
| 135 | +#### 3. Updating documentation |
| 136 | + |
| 137 | +Don't forget to add `new-theme` to the documentation so that other users are aware of your theme. |
| 138 | + |
| 139 | +To do this, find the theme section in all translations and add the new-theme parameter: |
| 140 | + |
| 141 | +* `docs/reference/configuration.md` |
| 142 | +* `docs/{lang}/reference/configuration.md` |
| 143 | + |
| 144 | +## Writing translations |
| 145 | + |
| 146 | +#### 1. Creating a new language file |
| 147 | + |
| 148 | +Create a new file in the `locales` folder. We recommend to copy `locales/en-US.json` and make translation based on it. |
| 149 | + |
| 150 | +#### 2. File connection |
| 151 | + |
| 152 | +After creating the language file, you need to plug it into `nuxt.config.ts` |
| 153 | + |
| 154 | +```typescript |
| 155 | +export default defineNuxtConfig({ |
| 156 | + i18n: { |
| 157 | + locales: [ |
| 158 | + { // [!code focus] |
| 159 | + code: 'en', // [!code focus] |
| 160 | + iso: 'en-US', // [!code focus] |
| 161 | + name: 'English', // [!code focus] |
| 162 | + file: 'en-US.json', // [!code focus] |
| 163 | + }, // [!code focus] |
| 164 | + ], |
| 165 | + }, |
| 166 | +}) |
| 167 | +``` |
| 168 | + |
| 169 | +#### 3. Updating documentation |
| 170 | + |
| 171 | +Don't forget to add the new language to the documentation so that other users are aware of the translation. |
| 172 | + |
| 173 | +To do this, find the language section in all translations and add a new parameter: |
| 174 | + |
| 175 | +* `docs/reference/configuration.md` |
| 176 | +* `docs/{lang}/reference/configuration.md` |
| 177 | + |
| 178 | +## Updating dependencies |
| 179 | + |
| 180 | +This is not necessary. The project has [Dependabot](https://github.com/dependabot), which scans for new dependencies once a week. |
| 181 | +But if you still have such a need, run the `yarn upgrade` command. After that, check everything that |
| 182 | +has been updated. |
| 183 | + |
| 184 | +Once you are sure that the application works, create a new `pull request`. |
0 commit comments