Design Patterns
There are a few design patterns that are used throughout the SDK that help with reusability and scalability. The following sections will explain these patterns and how to use them.
The use() function
This function initializes a User, Graph, or Audit Logs client based on the token set you pass it. Requests made with that instance use that token set.
// Graph API
edlink.use({
access_token: '[...]',
type: TokenSetType.Integration
});
// User API
edlink.use({
access_token: '[...]',
refresh_token: '[...]',
expiration_date: Date.now();
type: TokenSetType.Person
});
// Meta API
edlink.meta.audit_events.create({...});
// or
edlink.use({
access_token: '[...]',
type: TokenSetType.Application
});
edlink.meta is authenticated with the client_secret from the constructor. Use it for audit events and audit sessions.
Using async generators
This SDK makes use of generators to simplify pagination. Here is an example of how to use one:
const schools: Map<string, School> = new Map();
for await (const school of edlink.use(token_set).schools.list({ limit: 20 })) {
schools.set(school.id, school);
}
console.log(schools.size); // 20
Error handling for generators is a little different. Click the link below to read more about it.
Paging Arguments
Some functions paginate requests and accept a configuration object allowing you to customize this behavior. For example:
edlink.use(token_set).schools.list({
limit: 50
});
| Parameter | Type | Description |
|---|---|---|
limit | number | The max number of items to recieve. |
Filtering
Some functions accept a filter object allowing you to filter the results the API returns. For example:
for await (const person of edlink.use(token_set).people.list({
filter: {
first_name: [{ operator: 'starts with', value: 'Lil' }]
}
})) {
console.log(person.first_name); // Lilly, Lillian, etc.
}
