@innobrain/onoffice-adapter-js
v0.2.0
Published
OnOffice API adapter for JavaScript projects
Readme
@innobrain/onoffice-adapter-js
A JavaScript/TypeScript client for the onOffice API, modeled after the Laravel adapter.
Installation
npm install @innobrain/onoffice-adapter-jsUsage
import {
OnOfficeAction,
OnOfficeClient,
OnOfficeResourceType,
createRepositories,
} from "@innobrain/onoffice-adapter-js";
const client = new OnOfficeClient({
token: process.env.ON_OFFICE_TOKEN ?? "",
secret: process.env.ON_OFFICE_SECRET ?? "",
apiClaim: process.env.ON_OFFICE_API_CLAIM,
});
const { raw, estate, address, activity, fields, searchCriteria, file, marketplace, settings } =
createRepositories(client);
const estates = await estate
.query()
.select(["Id", "kaufpreis"])
.where("status", 1)
.orderByField("kaufpreis")
.get();
// Count records without fetching
const totalEstates = await estate.query().where("status", 1).count();
// Iterate over all records
await estate.query().select(["Id"]).each((record) => {
console.log(record.id);
});
// Delete a record
await estate.query().delete(12345);
// Raw repository for custom resource types
const custom = await raw.query(OnOfficeResourceType.Estate).select(["Id"]).find(12345);
const uploaded = await file.upload().uploadInBlocks(20480).saveAndLink("base64EncodedFile", {
module: "estate",
relatedRecordId: 12345,
file: "offer.pdf",
Art: "Dokument",
});
const firstEstate = await estate.query().select(["Id"]).find(12345);
const rawResponse = await client.request(OnOfficeAction.Read, OnOfficeResourceType.Estate, {
parameters: {
data: ["Id"],
},
});
const addresses = await address.query().select(["Name", "Vorname"]).get();
const activities = await activity
.query()
.estateId(123)
.addressIds([456, 789])
.select(["Nr", "Aktionsart", "Aktionstyp", "Datum", "Bemerkung"])
.get();
const fieldDefinitions = await fields.query().withModules(["estate", "address"]).get();
const criteria = await searchCriteria.query().mode("searchcriteria").ids([29]).get();
const criteriaFields = await searchCriteria.fields().get();
const criteriaMatches = await searchCriteria
.search()
.searchData({ range_plz: "52074" })
.outputAll()
.get();
const estateFiles = await file.estateFiles(12345, { includeImageUrl: "small" });
const addressFiles = await file.addressFiles(6789);
const users = await settings.users().select(["Nr", "Vorname"]).get();
const regions = await settings.regions().get();
const imprint = await settings.imprint().select(["Id"]).first();
const actions = await settings.actions().get();
const invoiceRecipient = await marketplace.invoiceRecipient({
transactionId: 1100135697,
userId: 42,
});Repository classes are exported (RawRepository, EstateRepository, AddressRepository,
ActivityRepository, FieldRepository, SearchCriteriaRepository, FileRepository,
MarketplaceRepository, SettingRepository) if you want to wire your own container.
Notes
- Provide a
fetcherin the client config if your runtime does not exposefetch(Node < 18). - Retries happen only on network errors by default; set
retry.onlyOnNetworkErrortofalsefor all errors. Non-2xx HTTP responses throwOnOfficeErrorwith status details. - Query builders are generic so you can type records (e.g.
estate.query<MyEstate>()). - Query builders support Eloquent-style methods:
where(),whereIn(),first(),get(),find(),create(),modify(),delete(),count(),each(). - The
where()method accepts typed operators:=,!=,<,>,<=,>=,in,not in,between,like,not like. - File access uses
file.estateFiles/file.addressFilesand requires a record ID. - File uploads for the
estatemodule require anArtvalue (e.g.Dokument,Foto). - Pagination defaults to
MAX_PAGE_SIZE(500), which is also exported as a constant.
Tooling
npm run build
npm run test
npm run lint
npm run formatLocal testing
Create a .env in onoffice-adapter-js with:
ON_OFFICE_TOKEN=...
ON_OFFICE_SECRET=...
ON_OFFICE_API_CLAIM=... # optionalThen run:
npm install
npm run dev