refac: define typed symbol for provider

This commit is contained in:
Dmitriy Pleshevskiy 2023-02-01 00:09:33 +03:00
parent 18eda9bb5d
commit ba555590ee
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
8 changed files with 33 additions and 45 deletions

View file

@ -1,16 +1,14 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { reactive } from "vue";
import { useContactsStore } from "../store";
import type { ContactFormApiCreateProps, ContactFormApiPort } from "./ports";
export const CONTACT_FORM_API_PROVIDE_KEY = Symbol("ContactFormApi");
export const CONTACT_FORM_API_PROVIDE_KEY =
defineInjectKey<ContactFormApiPort>("ContactFormApi");
export function useContactFormStore() {
const api = validInject<ContactFormApiPort>(
CONTACT_FORM_API_PROVIDE_KEY,
"ContactFormApiPort"
);
const api = validInject(CONTACT_FORM_API_PROVIDE_KEY);
const contactsStore = useContactsStore();

View file

@ -1,5 +1,5 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { storeToRefs } from "pinia";
import { reactive } from "vue";
import { useContactsStore } from "../store";
@ -8,13 +8,11 @@ import type {
ContactsTableApiPort,
} from "./ports";
export const CONTACTS_TABLE_API_PROVIDE_KEY = Symbol("ContactsTableApi");
export const CONTACTS_TABLE_API_PROVIDE_KEY =
defineInjectKey<ContactsTableApiPort>("ContactsTableApi");
export function useContactsTableStore() {
const api = validInject<ContactsTableApiPort>(
CONTACTS_TABLE_API_PROVIDE_KEY,
"ContactsTableApiPort"
);
const api = validInject(CONTACTS_TABLE_API_PROVIDE_KEY);
const contactsStore = useContactsStore();
const { contacts } = storeToRefs(contactsStore);

View file

@ -1,17 +1,15 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { storeToRefs } from "pinia";
import { reactive } from "vue";
import { useFilesStore } from "../store";
import type { FilesTableApiPort } from "./ports";
export const FILES_TABLE_API_PROVIDE_KEY = Symbol("FilesTableApi");
export const FILES_TABLE_API_PROVIDE_KEY =
defineInjectKey<FilesTableApiPort>("FilesTableApi");
export function useFilesTableStore() {
const api = validInject<FilesTableApiPort>(
FILES_TABLE_API_PROVIDE_KEY,
"ContactsTableApiPort"
);
const api = validInject(FILES_TABLE_API_PROVIDE_KEY);
const filesStore = useFilesStore();
const { files } = storeToRefs(filesStore);

View file

@ -1,16 +1,14 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { reactive } from "vue";
import { useListsStore } from "../store";
import type { ListFormApiCreateProps, ListFormApiPort } from "./ports";
export const LIST_FORM_API_PROVIDE_KEY = Symbol("ListFormApi");
export const LIST_FORM_API_PROVIDE_KEY =
defineInjectKey<ListFormApiPort>("ListFormApi");
export function useListFormStore() {
const api = validInject<ListFormApiPort>(
LIST_FORM_API_PROVIDE_KEY,
"ListFormApiPort"
);
const api = validInject(LIST_FORM_API_PROVIDE_KEY);
const listsStore = useListsStore();

View file

@ -1,16 +1,14 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { computed, reactive } from "vue";
import { useListsStore } from "../store";
import type { ListsSelectApiPort } from "./ports";
export const LISTS_SELECT_API_PROVIDE_KEY = Symbol("ListsTableApi");
export const LISTS_SELECT_API_PROVIDE_KEY =
defineInjectKey<ListsSelectApiPort>("ListsTableApi");
export function useListsSelectStore() {
const api = validInject<ListsSelectApiPort>(
LISTS_SELECT_API_PROVIDE_KEY,
"ListsSelectApiPort"
);
const api = validInject(LISTS_SELECT_API_PROVIDE_KEY);
const listsStore = useListsStore();

View file

@ -1,17 +1,15 @@
import { useLoader } from "@/shared/lib/composables/loader";
import { validInject } from "@/shared/lib/di";
import { defineInjectKey, validInject } from "@/shared/lib/di";
import { storeToRefs } from "pinia";
import { reactive } from "vue";
import { useListsStore } from "../store";
import type { ListsTableApiPort } from "./ports";
export const LISTS_TABLE_API_PROVIDE_KEY = Symbol("ListsTableApi");
export const LISTS_TABLE_API_PROVIDE_KEY =
defineInjectKey<ListsTableApiPort>("ListsTableApi");
export function useListsTableStore() {
const api = validInject<ListsTableApiPort>(
LISTS_TABLE_API_PROVIDE_KEY,
"ContactsTableApiPort"
);
const api = validInject(LISTS_TABLE_API_PROVIDE_KEY);
const listsStore = useListsStore();
const { lists } = storeToRefs(listsStore);

View file

@ -25,7 +25,7 @@ function openAddContactForm() {
const selectedListId = ref("");
watch(
() => route.params.listId,
(listId) => {
(listId: string) => {
selectedListId.value = listId ?? "";
},
{ immediate: true }

View file

@ -1,15 +1,15 @@
import { inject } from "vue";
import { inject, type InjectionKey } from "vue";
export function validInject<T>(key: Symbol, interfaceName?: string): T {
const api = inject<T>(key);
export function defineInjectKey<T>(name: string): InjectionKey<T> {
return Symbol(name);
}
export function validInject<T>(key: InjectionKey<T>): T {
const api = inject(key);
if (api == null) {
let errorMessage = `You have to provide "${key.toString()}"`;
if (interfaceName) {
errorMessage += `, that implement ${interfaceName} interface`;
}
const errorMessage = `You have to provide "${key.toString()}"`;
throw new Error(errorMessage);
}
// TODO: it would be great if we could check the type for the injected object.
return api;
}