nuxt-outfit 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,89 @@
1
+ <!--
2
+ Get your module up and running quickly.
3
+
4
+ Find and replace all on all files (CMD+SHIFT+F):
5
+ - Name: My Module
6
+ - Package name: my-module
7
+ - Description: My new Nuxt module
8
+ -->
9
+
10
+ # @nuxtjs/outfit
11
+
12
+ [![npm version][npm-version-src]][npm-version-href]
13
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
14
+ [![License][license-src]][license-href]
15
+
16
+ > My new Nuxt module
17
+
18
+ - [✨ &nbsp;Release Notes](/CHANGELOG.md)
19
+ <!-- - [📖 &nbsp;Documentation](https://example.com) -->
20
+
21
+ ## Features
22
+
23
+ <!-- Highlight some of the features your module provide here -->
24
+ - ⛰ &nbsp;Foo
25
+ - 🚠 &nbsp;Bar
26
+ - 🌲 &nbsp;Baz
27
+
28
+ ## Quick Setup
29
+
30
+ 1. Add `my-module` dependency to your project
31
+
32
+ ```bash
33
+ # Using pnpm
34
+ pnpm add -D my-module
35
+
36
+ # Using yarn
37
+ yarn add --dev my-module
38
+
39
+ # Using npm
40
+ npm install --save-dev my-module
41
+ ```
42
+
43
+ 2. Add `my-module` to the `modules` section of `nuxt.config.ts`
44
+
45
+ ```js
46
+ export default defineNuxtConfig({
47
+ modules: [
48
+ 'my-module'
49
+ ]
50
+ })
51
+ ```
52
+
53
+ That's it! You can now use My Module in your Nuxt app ✨
54
+
55
+ ## Development
56
+
57
+ ```bash
58
+ # Install dependencies
59
+ npm install
60
+
61
+ # Generate type stubs
62
+ npm run dev:prepare
63
+
64
+ # Develop with the playground
65
+ npm run dev
66
+
67
+ # Build the playground
68
+ npm run dev:build
69
+
70
+ # Run ESLint
71
+ npm run lint
72
+
73
+ # Run Vitest
74
+ npm run test
75
+ npm run test:watch
76
+
77
+ # Release new version
78
+ npm run release
79
+ ```
80
+
81
+ <!-- Badges -->
82
+ [npm-version-src]: https://img.shields.io/npm/v/my-module/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
83
+ [npm-version-href]: https://npmjs.com/package/my-module
84
+
85
+ [npm-downloads-src]: https://img.shields.io/npm/dm/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
86
+ [npm-downloads-href]: https://npmjs.com/package/my-module
87
+
88
+ [license-src]: https://img.shields.io/npm/l/my-module.svg?style=flat&colorA=18181B&colorB=28CF8D
89
+ [license-href]: https://npmjs.com/package/my-module
@@ -0,0 +1,5 @@
1
+ module.exports = function(...args) {
2
+ return import('./module.mjs').then(m => m.default.call(this, ...args))
3
+ }
4
+ const _meta = module.exports.meta = require('./module.json')
5
+ module.exports.getMeta = () => Promise.resolve(_meta)
@@ -0,0 +1,13 @@
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+
3
+ declare const _default: _nuxt_schema.NuxtModule<{
4
+ echo: {
5
+ broadcaster: string;
6
+ autoConnect: boolean;
7
+ transports: string[];
8
+ };
9
+ serverUrl: null;
10
+ httpInstance: null;
11
+ }>;
12
+
13
+ export { _default as default };
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "@nuxt/outfit",
3
+ "configKey": "outfit",
4
+ "version": "1.0.1"
5
+ }
@@ -0,0 +1,30 @@
1
+ import { createResolver, defineNuxtModule, addPlugin } from '@nuxt/kit';
2
+
3
+ const { resolve } = createResolver(import.meta.url);
4
+ const module = defineNuxtModule({
5
+ meta: {
6
+ name: "@nuxt/outfit",
7
+ configKey: "outfit"
8
+ },
9
+ defaults: {
10
+ echo: {
11
+ broadcaster: "socket.io",
12
+ autoConnect: false,
13
+ transports: ["websocket"]
14
+ },
15
+ serverUrl: null,
16
+ httpInstance: null
17
+ },
18
+ hooks: {
19
+ "imports:dirs": (dirs) => {
20
+ dirs.push(resolve("./runtime/composables"));
21
+ }
22
+ },
23
+ setup(options, nuxt) {
24
+ nuxt.options.runtimeConfig.public.outfit = { echo: options.echo, httpInstance: options.httpInstance };
25
+ nuxt.options.runtimeConfig.outfit = { serverUrl: options.serverUrl };
26
+ addPlugin(resolve("./runtime/plugins/echo.client"));
27
+ }
28
+ });
29
+
30
+ export { module as default };
@@ -0,0 +1,3 @@
1
+ export declare const useDetectOutsideClick: (component: any, callback: any) => {
2
+ listener: (event: any) => void;
3
+ } | undefined;
@@ -0,0 +1,22 @@
1
+ export const useDetectOutsideClick = (component, callback) => {
2
+ if (!component) {
3
+ return;
4
+ }
5
+ const listener = (event) => {
6
+ if (event.target !== component.value && event.composedPath().includes(component.value)) {
7
+ return;
8
+ }
9
+ if (typeof callback === "function") {
10
+ callback();
11
+ }
12
+ };
13
+ onMounted(() => {
14
+ window.addEventListener("click", listener);
15
+ });
16
+ onBeforeUnmount(() => {
17
+ window.removeEventListener("click", listener);
18
+ });
19
+ return {
20
+ listener
21
+ };
22
+ };
@@ -0,0 +1,45 @@
1
+ export declare const useEcho: () => {
2
+ /**
3
+ * After success connected
4
+ *
5
+ * @param callback
6
+ */
7
+ connected: (callback: any) => void;
8
+ /**
9
+ * Connect to the socket
10
+ *
11
+ */
12
+ connect: () => void;
13
+ /**
14
+ * Disconnect from sockets
15
+ *
16
+ */
17
+ disconnect: () => void;
18
+ /**
19
+ * Public channel
20
+ *
21
+ * @param channel
22
+ * @param events
23
+ */
24
+ joinPublic: (channel: any, events: any) => void;
25
+ /**
26
+ * Private channel
27
+ *
28
+ * @param channel
29
+ * @param events
30
+ */
31
+ joinPrivate: (channel: any, events: any) => void;
32
+ /**
33
+ * Presence channel
34
+ *
35
+ * @param channel
36
+ * @param events
37
+ */
38
+ joinPresence: (channel: any, events: any) => void;
39
+ /**
40
+ * Leave from channel
41
+ *
42
+ * @param channel
43
+ */
44
+ leave: (channel: any) => void;
45
+ };
@@ -0,0 +1,73 @@
1
+ export const useEcho = () => {
2
+ const { $echo } = useNuxtApp();
3
+ const notificationNamespace = ".Illuminate\\Notifications\\Events\\BroadcastNotificationCreated";
4
+ const listen = (room, events) => {
5
+ Object.keys(events).forEach((key) => {
6
+ room.listen(
7
+ key === "notification" ? notificationNamespace : key,
8
+ (payload) => events[key](payload)
9
+ );
10
+ });
11
+ };
12
+ return {
13
+ /**
14
+ * After success connected
15
+ *
16
+ * @param callback
17
+ */
18
+ connected: (callback) => {
19
+ $echo.connector.socket.on("connect", () => {
20
+ callback();
21
+ });
22
+ },
23
+ /**
24
+ * Connect to the socket
25
+ *
26
+ */
27
+ connect: () => {
28
+ $echo.connector.socket.connect();
29
+ },
30
+ /**
31
+ * Disconnect from sockets
32
+ *
33
+ */
34
+ disconnect: () => {
35
+ $echo.connector.socket.disconnect();
36
+ },
37
+ /**
38
+ * Public channel
39
+ *
40
+ * @param channel
41
+ * @param events
42
+ */
43
+ joinPublic: (channel, events) => {
44
+ listen($echo.channel(channel), events);
45
+ },
46
+ /**
47
+ * Private channel
48
+ *
49
+ * @param channel
50
+ * @param events
51
+ */
52
+ joinPrivate: (channel, events) => {
53
+ listen($echo.private(channel), events);
54
+ },
55
+ /**
56
+ * Presence channel
57
+ *
58
+ * @param channel
59
+ * @param events
60
+ */
61
+ joinPresence: (channel, events) => {
62
+ listen($echo.join(channel), events);
63
+ },
64
+ /**
65
+ * Leave from channel
66
+ *
67
+ * @param channel
68
+ */
69
+ leave: (channel) => {
70
+ $echo.leaveChannel(channel);
71
+ }
72
+ };
73
+ };
@@ -0,0 +1,12 @@
1
+ export declare const useForm: (opts?: {}) => {
2
+ fields: any;
3
+ errors: any;
4
+ repeatArray: (property: any) => void;
5
+ removeArray: (property: any, index: any) => void;
6
+ isPending: any;
7
+ handleSubmit: () => Promise<void>;
8
+ clearError: (field: any) => void;
9
+ reset: () => void;
10
+ onSuccess: (callback: any) => void;
11
+ onFail: (callback: any) => void;
12
+ };
@@ -0,0 +1,102 @@
1
+ import { klona as deepClone } from "klona/full";
2
+ import { unflatten } from "flat";
3
+ import { has, unset } from "lodash-es";
4
+ export const useForm = (opts = {}) => {
5
+ const httpInstance = useRuntimeConfig().public.outfit.httpInstance;
6
+ const http = useNuxtApp()?.[httpInstance] ?? $fetch;
7
+ const snackMessage = useState("snackMessage");
8
+ const fields = reactive(deepClone(opts?.initialValues));
9
+ const isPending = ref(false);
10
+ const errors = ref({});
11
+ let _onSuccess = null;
12
+ let _onFail = null;
13
+ const onSuccess = (callback) => {
14
+ _onSuccess = callback;
15
+ };
16
+ const onFail = (callback) => {
17
+ _onFail = callback;
18
+ };
19
+ const processing = () => {
20
+ isPending.value = true;
21
+ };
22
+ const processed = () => {
23
+ isPending.value = false;
24
+ };
25
+ const reset = () => {
26
+ Object.assign(fields, deepClone(opts?.initialValues));
27
+ };
28
+ const repeatArray = (property) => {
29
+ if (Array.isArray(fields[property])) {
30
+ fields[property].push(deepClone(opts?.initialValues[property][0]));
31
+ }
32
+ };
33
+ const removeArray = (property, index) => {
34
+ if (Array.isArray(fields[property])) {
35
+ fields[property].splice(index, 1);
36
+ }
37
+ if (Array.isArray(errors.value[property])) {
38
+ errors.value[property].splice(index, 1);
39
+ }
40
+ };
41
+ const setErrors = (exceptions) => {
42
+ errors.value = exceptions;
43
+ };
44
+ const clearErrors = () => {
45
+ errors.value = {};
46
+ };
47
+ const clearError = (field) => {
48
+ if (has(errors.value, field)) {
49
+ unset(errors.value, field);
50
+ }
51
+ };
52
+ const handleFail = (fail, isRunCallback = true) => {
53
+ let messages = {};
54
+ const brackets = (text) => text.replace(/[[\].]+/g, ".");
55
+ if (fail.name === "ValidationError") {
56
+ if (fail.inner.length !== 0) {
57
+ for (const instance of fail.inner) {
58
+ messages[brackets(instance.path)] = instance.errors.map((item) => brackets(item));
59
+ }
60
+ } else {
61
+ messages[brackets(fail.path)] = brackets(fail.message);
62
+ }
63
+ } else if (fail.name === "FetchError") {
64
+ if (fail.data?.errors !== void 0) {
65
+ messages = fail.data.errors;
66
+ } else if (fail.data?.message !== void 0) {
67
+ snackMessage.value = fail.data?.message;
68
+ }
69
+ }
70
+ setErrors(unflatten(messages));
71
+ if (_onFail && isRunCallback) {
72
+ _onFail(errors);
73
+ }
74
+ };
75
+ const handleSuccess = (hit) => {
76
+ clearErrors();
77
+ reset();
78
+ if (_onSuccess) {
79
+ _onSuccess(hit);
80
+ }
81
+ };
82
+ const validate = () => {
83
+ return opts?.schema.validate(deepClone(fields), { abortEarly: false }).then((validated) => http(opts?.action, { method: "POST", body: validated })).then((hit) => handleSuccess(hit)).catch((fail) => handleFail(fail));
84
+ };
85
+ const handleSubmit = async () => {
86
+ processing();
87
+ await validate();
88
+ processed();
89
+ };
90
+ return {
91
+ fields,
92
+ errors,
93
+ repeatArray,
94
+ removeArray,
95
+ isPending,
96
+ handleSubmit,
97
+ clearError,
98
+ reset,
99
+ onSuccess,
100
+ onFail
101
+ };
102
+ };
@@ -0,0 +1,8 @@
1
+ export declare const useSpaHeaders: (additionalHeaders?: {}) => {
2
+ headers: any;
3
+ onResponse({ response }: {
4
+ response: any;
5
+ }): Promise<void>;
6
+ } | {
7
+ headers: {};
8
+ };
@@ -0,0 +1,37 @@
1
+ import { appendHeader } from "h3";
2
+ import { parse, splitCookiesString as split } from "set-cookie-parser";
3
+ import { serialize } from "cookie";
4
+ export const useSpaHeaders = (additionalHeaders = {}) => {
5
+ if (process.client) {
6
+ return {
7
+ headers: additionalHeaders
8
+ };
9
+ }
10
+ const serverUrl = useRuntimeConfig().outfit.serverUrl;
11
+ const event = useRequestEvent();
12
+ const headers = useRequestHeaders(["cookie", "referer", "host"]);
13
+ const goodResponses = [200, 201, 202, 203, 204, 205, 206, 207, 208, 226];
14
+ if (!Object.prototype.hasOwnProperty.call(headers, "referer")) {
15
+ headers.referer = `${headers.host}/${event.req.originalUrl}`;
16
+ }
17
+ const parseCookie = (cookies) => {
18
+ return parse(split(cookies)).map((cookie) => serialize(cookie.name, cookie.value, cookie));
19
+ };
20
+ const serverOptions = {
21
+ headers: {
22
+ ...additionalHeaders,
23
+ ...headers
24
+ },
25
+ // eslint-disable-next-line
26
+ async onResponse({ response }) {
27
+ const { status, headers: headers2 } = response;
28
+ if (goodResponses.includes(status)) {
29
+ appendHeader(event, "Set-Cookie", parseCookie(headers2.get("set-cookie")));
30
+ }
31
+ }
32
+ };
33
+ if (serverUrl) {
34
+ serverOptions.baseURL = serverUrl;
35
+ }
36
+ return serverOptions;
37
+ };
@@ -0,0 +1,2 @@
1
+ declare const _default: any;
2
+ export default _default;
@@ -0,0 +1,10 @@
1
+ import { defineNuxtPlugin, useRuntimeConfig } from "#imports";
2
+ import { io } from "socket.io-client";
3
+ import Echo from "laravel-echo";
4
+ export default defineNuxtPlugin((nuxtApp) => {
5
+ const options = useRuntimeConfig().public.outfit.echo;
6
+ if (options.broadcaster === "socket.io") {
7
+ options.client = io;
8
+ }
9
+ nuxtApp.provide("echo", new Echo(options));
10
+ });
@@ -0,0 +1,6 @@
1
+
2
+ import { } from './module'
3
+
4
+
5
+
6
+ export { default } from './module'
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "nuxt-outfit",
3
+ "version": "1.0.1",
4
+ "description": "Outfit - Connection Laravel and Nuxt",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/k0mar12/outfit"
8
+ },
9
+ "homepage": "https://github.com/k0mar12/outfit",
10
+ "author": "Vlad Poshvaniuk",
11
+ "contributors": [
12
+ "k0mar12 <vladlovedance@gmail.com>"
13
+ ],
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/types.d.ts",
19
+ "import": "./dist/module.mjs",
20
+ "require": "./dist/module.cjs"
21
+ }
22
+ },
23
+ "main": "./dist/module.cjs",
24
+ "types": "./dist/types.d.ts",
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "scripts": {
29
+ "prepack": "nuxt-module-build",
30
+ "dev": "nuxi dev playground",
31
+ "dev:build": "nuxi build playground",
32
+ "dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
33
+ "release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
34
+ "lint": "eslint .",
35
+ "test": "vitest run",
36
+ "test:watch": "vitest watch"
37
+ },
38
+ "dependencies": {
39
+ "@nuxt/kit": "^3.2.2",
40
+ "cookie": "^0.5.0",
41
+ "flat": "^5.0.2",
42
+ "klona": "^2.0.6",
43
+ "laravel-echo": "^1.15.0",
44
+ "lodash-es": "^4.17.21",
45
+ "set-cookie-parser": "^2.5.1",
46
+ "socket.io-client": "^4.6.0",
47
+ "yup": "^1.0.0"
48
+ },
49
+ "devDependencies": {
50
+ "@nuxt/eslint-config": "^0.1.1",
51
+ "@nuxt/module-builder": "^0.2.1",
52
+ "@nuxt/schema": "^3.2.2",
53
+ "@nuxt/test-utils": "^3.2.2",
54
+ "changelogen": "^0.4.1",
55
+ "eslint": "^8.34.0",
56
+ "nuxt": "^3.2.2",
57
+ "vitest": "^0.28.5"
58
+ },
59
+ "keywords": [
60
+ "outfit",
61
+ "nuxt",
62
+ "laravel",
63
+ "module"
64
+ ]
65
+ }