🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

payload-client

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

payload-client

Basic js client to easily interact with the Payload CMS Rest API

latest
Source
npmnpm
Version
1.0.8
Version published
Weekly downloads
5
150%
Maintainers
0
Weekly downloads
 
Created
Source

Payload Client

A simple javascript client for your payload CMS api. Designed to rapidly extend your headless CMS with your favorite js framework

Installation

npm i --save payload-client

Configuration

First let's create a file to define our basic client config + auth adapter

payload.js

import { createClient, useAuth } from 'payload-client'

const client = createClient('http://localhost:3000');
const auth = useAuth(client);

export { client, auth }

Usage

Now that we've configured our client, let's try it out in any other file

import { auth, client } from './payload.js'

(async () => {
	await auth.login('EMAIL', 'PASSWORD'); // you can catch errors here if you want, or just let it crash
	console.log('AUTHENTICATED:', auth.isAuthenticated()); // should return true
	console.log('USER', auth.getUser()); // should return your user object
	await client.service('users').find(); // should return all users (depending on api permissions)
	await auth.logout(); // you can catch errors here too
	console.log('AUTHENTICATED:', auth.isAuthenticated()); // should return false
	console.log('USER', auth.getUser()); // should return false
	await client.service('users').find(); // should return empty results (depending on api permissions)
});

Service Methods

find (options);
get (id, options);
create (data, options);
update (id, data, options);
delete (id, options);

The Options Argument

{
	where: {},
	/**
	 * Extra options get put into fetch options
	 *
	 * fetch(url, { ...overloadFetch })
	 * See Fetch Docs: https://developer.mozilla.org/en-US/docs/Web/API/fetch#options
	 */
	...overloadFetch
}

Auth Utilities

async login(username, password) => Promise<void>
async logout() => Promise<void>
isAuthenticated() => boolean
getUser() => Object | boolean

That's all it does for now, will update as it evolves

FAQs

Package last updated on 26 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts