Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
feat(chore): finished module functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Intevel committed Apr 23, 2022
1 parent f6d65b0 commit 9dff57f
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 18 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -32,7 +32,8 @@
"lint": "eslint --ext .js,.ts,.vue"
},
"dependencies": {
"@nuxt/kit": "^3.0.0-rc.1"
"@nuxt/kit": "^3.0.0-rc.1",
"defu": "^6.0.0"
},
"devDependencies": {
"@nuxt/module-builder": "latest",
Expand Down
49 changes: 35 additions & 14 deletions src/module.ts
@@ -1,24 +1,45 @@
import { resolve } from 'path'
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin } from '@nuxt/kit'
import { resolve } from "path";
import { fileURLToPath } from "url";
import { defineNuxtModule, addPlugin } from "@nuxt/kit";
import defu from "defu";

export interface ModuleOptions {
addPlugin: boolean
/**
* Your LogSnag API Token -> https://app.logsnag.com/dashboard/settings/api
* @default process.env.LOGSNAG_API_TOKEN
* @type string
*/
token?: string;
}

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'my-module',
configKey: 'myModule'
name: "nuxt-logsnag",
configKey: "logsnag",
compatibility: {
nuxt: "^3.0.0 || ^2.16.0",
bridge: true,
},
},
defaults: {
addPlugin: true
token: process.env.LOGSNAG_API_TOKEN,
},
setup (options, nuxt) {
if (options.addPlugin) {
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin'))
setup(options, nuxt) {
if (!options.token) {
throw new Error("Missing LogSnag API Token");
}
}
})
nuxt.options.publicRuntimeConfig.logsnag = defu(
nuxt.options.publicRuntimeConfig.logsnag,
{
token: options.token,
}
);
const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
nuxt.options.build.transpile.push(runtimeDir);
addPlugin(resolve(runtimeDir, "plugin"));

nuxt.hook("autoImports:dirs", (dirs) => {
dirs.push(resolve(runtimeDir, "composables"));
});
},
});
28 changes: 28 additions & 0 deletions src/runtime/composables/useLogSnag.ts
@@ -0,0 +1,28 @@
import { LogSnagPublishOptions } from "../types";
import { useNuxtApp, useRuntimeConfig } from "#app";
const endpoint = "https://api.logsnag.com/v1/log";

export const useLogSnag = () => {
const nuxt = useNuxtApp();
const config = useRuntimeConfig();

const publishEvent = async (data: LogSnagPublishOptions) => {
try {
return await $fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.logsnag.token}`,
},
body: data,
});
} catch (err: any) {
console.error(err);
throw err;
}
};

return {
publishEvent,
};
};
6 changes: 3 additions & 3 deletions src/runtime/plugin.ts
@@ -1,5 +1,5 @@
import { defineNuxtPlugin } from '#app'
import { defineNuxtPlugin } from "#app";

export default defineNuxtPlugin((nuxtApp) => {
console.log('Plugin by my-module!')
})
console.log("[nuxt-logsnag]: Plugin loaded!");
});
37 changes: 37 additions & 0 deletions src/runtime/types/index.d.ts
@@ -0,0 +1,37 @@
export interface LogSnagPublishOptions {
/**
* Project name
* example: "my-saas"
*/
project: string;

/**
* Channel name
* example: "waitlist"
*/
channel: string;

/**
* Event name
* example: "User Joined"
*/
event: string;

/**
* Event description
* example: "joe@example.com joined waitlist"
*/
description?: string;

/**
* Event icon (emoji)
* must be a single emoji
* example: "馃帀"
*/
icon?: string;

/**
* Send push notification
*/
notify?: boolean;
}

0 comments on commit 9dff57f

Please sign in to comment.