Socket
Book a DemoInstallSign in
Socket

payload-client

Package Overview
Dependencies
Maintainers
1
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

Source
npmnpm
Version
1.0.4
Version published
Weekly downloads
4
33.33%
Maintainers
1
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 () => {
	// do a login, cookie will be stored and passed automatically by the client
	await auth.login('email', 'password');
	
	// query some users
	const { docs } = await client.service('users').find({ where: { email: { equals: 'myemail' } } });
	console.log(docs);
});

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 Nov 2023

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