This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chore): finished module functionality
- Loading branch information
Showing
5 changed files
with
105 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")); | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |