@lindle/sharepoint_requests

v0.1.20

Published

TypeScript knihovna pro práci se SharePoint REST API nad `axios`. Primární cíl je jednodušší skládání URL, bezpečnější CRUD a čitelnější API.

Readme

HTTP SharePoint Requests

TypeScript knihovna pro práci se SharePoint REST API nad axios. Primární cíl je jednodušší skládání URL, bezpečnější CRUD a čitelnější API.

Installation

npm install @lindle/sharepoint_requests

Quick Start

import createBase from '@lindle/sharepoint_requests';

type Task = {
  Title: string;
  Description?: string;
  CategoryRef?: number;
};

type DB = {
  LISTS: {
    Tasks: Task;
  };
};

const sp = createBase<DB>().create({
  baseURL: 'https://tenant.sharepoint.com/sites/demo',
});

API Overview

1. List scope (from) - doporučené

from(listName) vrací immutable scope. Každý scope drží vlastní endpoint/context, takže paralelní volání si navzájem nepřepisují stav.

const tasks = sp.from('Tasks');

const { data, meta } = await tasks.get({
  cols: ['Title'],
  limit: 10,
  orderBy: ['Title', 'asc'],
});

console.log(meta.url.toString());

2. CRUD položek

const tasks = sp.from('Tasks');

await tasks.create({
  Title: 'Prepare monthly report',
  Description: 'Draft v1 by Friday',
});

await tasks.update(15, {
  Title: 'Prepare monthly report (final)',
});

await tasks.delete(15);
// alias:
await tasks.remove(15);

3. Pole (fields) listu

const allFields = await sp.from('Tasks').fields().get();
const titleField = await sp.from('Tasks').fields({ fieldName: 'Title' }).get();

// shortcut:
const titleField2 = await sp.field('Tasks', 'Title');

4. Přílohy a soubory

const tasks = sp.from('Tasks');

const attachment = new File(['hello'], "O'Brien.txt");
await tasks.createFile({ file: attachment, itemId: 10 });

const docs = sp.from('Tasks', '2013');
const doc = new File(['contract body'], 'contract.pdf');

await docs.uploadFile({
  file: doc,
  folderPath: ['Shared', 'Contracts', '2026'],
  overwrite: true,
});

await docs.deleteFile({
  fileName: 'contract.pdf',
  folderPath: ['Shared', 'Contracts', '2026'],
});

files namespace alias:

await sp.from('Tasks').files.attach({ file: attachment, itemId: 10 });
await sp.from('Tasks').files.upload({ file: doc });
await sp.from('Tasks').files.remove({ fileName: 'contract.pdf' });

5. Folder endpoint

const folderData = await sp.folder(['Shared Documents', 'Invoices']).get({
  type: 'Files',
  cols: ['Name'],
  limit: 20,
});

// default type je Files:
const folderData2 = await sp.folder('Shared Documents/Invoices').get();

6. Users + Email

const currentUser = await sp.getCurrentUser();
const profile = await sp.currentUserProperties();
const permission = await sp.currentUserPermission();

const siteUsers = await sp.users({ limit: 25 }).get();
const searchResult = await sp.user.searchUser('john.doe');
const matchedSiteUsers = await sp.user.searchSiteUser("o'connor");

await sp.sendEmail({
  From: '[email protected]',
  To: ['[email protected]'],
  Subject: 'Build status',
  Body: 'Deployment completed.',
});

// kompatibilní shortcut:
await sp.email(
  '[email protected]',
  ['[email protected]'],
  'Build status',
  'Deployment completed.'
);

SharePoint Version

Default je 2013.

await sp.from('Tasks', '2013').get();
await sp.from('Tasks', '2010').get();

list(listName) je stále dostupné jako deprecated alias na from(listName, '2010').

Response Shape

Kolekce vrací:

{
  data: ...,
  meta: { url: URL }
}

To zjednodušuje debug výsledné OData URL.

Notes

  • Knihovna předpokládá, že autentizaci řeší prostředí (cookies/tokeny).
  • Write operace používají X-RequestDigest.
  • Názvy listů, fieldů a souborů se interně escapují pro OData string literal.

License

MIT (viz LICENSE).