All files / src/utils tags.ts

43.47% Statements 10/23
0% Branches 0/1
0% Functions 0/9
40% Lines 6/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 391x 1x               1x                 1x                         1x       1x      
import joplin from "api";
import { fetchAllItems } from "./dataApi";
import { Note } from "./templates";
 
export interface Tag {
    id: string;
    title: string;
}
 
export const getAllTagsWithTitle = async (title: string): Promise<Tag[]> => {
    return (await fetchAllItems(["search"], { query: title, type: "tag" })).map(tag => {
        return {
            id: tag.id,
            title: tag.title
        }
    });
}
 
export const getAnyTagWithTitle = async (title: string): Promise<Tag> => {
    const existingTags = await getAllTagsWithTitle(title);
    Iif (existingTags.length) {
        return existingTags[0];
    }
 
    const tag = await joplin.data.post(["tags"], null, { title: title });
    return {
        id: tag.id,
        title: tag.title
    };
}
 
export const getAllNotesWithTag = async (tagId: string): Promise<Note[]> => {
    return fetchAllItems(["tags", tagId, "notes"], { fields: ["id", "title", "body"] });
}
 
export const applyTagToNote = async (tagId: string, noteId: string): Promise<void> => {
    await joplin.data.post(["tags", tagId, "notes"], null, { id: noteId });
}