Jakub Zych 6a5f6ce469 test: every friends client method's refusal and answer, error bodies
- each method throws FriendsApiError with the status and message
- the remaining unwrapped answers (members, sent, request, invites,
  groups, join by code)
- non-JSON, empty and malformed error bodies; a throwing token getter
2026-11-01 19:22:11 +01:00

vue-userfriends-layer

A Nuxt layer with friends and friend groups of the SummerCMS userfriends plugin: the friends list, incoming and sent requests, adding a friend, and the user's groups with their members and invite codes.

export default defineNuxtConfig({ extends: ['vue-userfriends-layer'] })

Overview

golem15.userfriends answers a JSON API under /_userfriends/api/v1, behind the user's frontend JWT. The layer renders it in one component, <FriendsPage>, with four sections:

  • Friend requests: received requests with Accept and Reject, sent ones with Cancel;
  • Your friends: accepted friends with Unfriend;
  • Add a friend: a picker of the people the app lists (the $userfriendsDirectory hook), or a username or e-mail field without it;
  • Friend groups: the user's groups with their members, Leave or Delete, the invite code for owners and admins, a form to create a group (open, or invite-only) and joining by invite code.

The plugin answers a generic 422 when it refuses a friend request (it does not tell whether a user exists); the layer shows the server's message as it is. Names and descriptions render as text.

Installation

Add the layer as a git dependency pinned to a tag, next to the Nuxt and Vue versions it is built for:

{
  "dependencies": {
    "nuxt": "4.4.8",
    "vue": "3.5.35",
    "vue-userfriends-layer": "git+https://git.golem15.com/golem15/vue-userfriends-layer.git#v0.1.0"
  }
}

The layer declares @nuxtjs/i18n (10.4.0), so the app does not list it again. It registers its friends.* copy only for the locales the app enables (en, pl). The browser calls the API on the site's own origin: route /_userfriends to the application (a dev proxy locally, the web server in production).

Runtime config

Key Env Default Purpose
userfriends.apiBase NUXT_USERFRIENDS_API_BASE http://127.0.0.1:8096/_userfriends/api/v1 Server-only API base, for an app page that fetches friends data on the server
public.userfriends.apiBase NUXT_PUBLIC_USERFRIENDS_API_BASE /_userfriends/api/v1 Browser base of golem15.userfriends

Usage

The components fetch with the user's token from the browser, so render them on the client only:

<!-- pages/friends.vue -->
<template>
  <ClientOnly v-if="loggedIn"><FriendsPage /></ClientOnly>
  <NuxtLink v-else to="/register">Register to add friends</NuxtLink>
</template>

Hooks

The app provides them from a Nuxt plugin:

// plugins/friends.ts
export default defineNuxtPlugin(() => ({
  provide: {
    // The user's frontend JWT, or null.
    userfriendsAuth: () => readTokenCookie(),
    // Optional: the people the user may befriend, for the add-friend picker.
    userfriendsDirectory: async () => {
      const res = await $fetch<{ people: { id: number; name: string }[] }>('/_acme/api/v1/people', {
        headers: { Authorization: `Bearer ${readTokenCookie()}` },
      })
      return res.people
    },
  },
}))

Every request carries Authorization: Bearer {token}, never a cookie. The layer never stores the token. The picker leaves out current friends and people with a pending request.

Components

Component Purpose
FriendsPage The four sections; loads everything on mount
FriendsRequests Received (Accept, Reject) and sent (Cancel) requests
FriendsList Accepted friends with Unfriend
FriendsAddFriend Directory picker or username/e-mail field; sends the request
FriendsGroups The user's groups, members, invite codes, create, join by code, leave, delete

The sections share one state (useFriends), so each can also be placed on its own.

API reference

  • createFriendsClient({ base, token, fetch? }) (app/utils/friendsClient.ts): friends, incoming, sent, request(target) ({user_id}, {username} or {email}), accept, reject, cancel, unfriend, groups, group, createGroup, updateGroup, deleteGroup, join, leave, members, kick, invite, mintInvite, revokeInvite, previewCode and joinByCode. Refusals throw FriendsApiError with status, message, errors, field(name) and firstError(). friendTarget(handle) turns a typed handle into {email} or {username}; friendsError(status, json) maps an error body.
  • useFriendsApi(): the client bound to runtime config and $userfriendsAuth; useFriendsAuth() and useFriendsDirectory(): the hooks.
  • useFriends(): friends, incoming, sent and groups in useState (keys userfriends:*), with the actions the components use.

Styling

app/assets/css/friends-base.css uses --friends-* variables with defaults (font, colors, radius, gap). Set them in the app's css to restyle the screens, or replace a component with a same-name file. Class names carry the friends- prefix.

Testing

pnpm install
pnpm test              # node --test: every client method's URL, body, answer and refusal, the Bearer header, the 422 and error-body mapping
pnpm build:playground  # builds .playground against the layer
Description
No description provided
Readme 145 KiB
Languages
TypeScript 61.5%
Vue 31.9%
CSS 6.6%