Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: use camelCase instead of snake_case in fields (Tigua002) #91

Merged
merged 26 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 42 additions & 40 deletions src/abax-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ export class AbaxClient {
return this.performRequest(apiKey => call({ input, apiKey }));
}

/** Gets paged list of Trips. Required scopes: `abax_profile`, `open_api`, `open_api.trips`. */
async listTrips(input: ListTripsInput): Promise<ListTripsResponse> {
if (input.query.page_size === 0) {
if (input.query.pageSize === 0) {
const trips = await this.listNextPagesOfTrips(input, 1);

return { page: 1, page_size: 0, items: trips };
return { page: 1, pageSize: 0, items: trips };
}

return this.listTripsPage(input);
Expand All @@ -113,13 +114,12 @@ export class AbaxClient {
.args<{ input: GetUsageSummaryInput }>()
.method('get')
.path(
({ input: { vehicle_id } }) =>
`/v1/vehicles/${vehicle_id}/usage-summary`,
({ input: { vehicleId } }) => `/v1/vehicles/${vehicleId}/usage-summary`,
)
.query(({ input }) => {
const queryParams = new URLSearchParams();
queryParams.append('from', format(input.date_from, 'yyyy-MM-dd'));
queryParams.append('to', format(input.date_to, 'yyyy-MM-dd'));
queryParams.append('from', format(input.dateFrom, 'yyyy-MM-dd'));
queryParams.append('to', format(input.dateTo, 'yyyy-MM-dd'));
return queryParams;
})
.parseJson(withZod(usageSummarySchema))
Expand All @@ -131,7 +131,7 @@ export class AbaxClient {
async listTripExpenses(
input: ListTripExpensesInput,
): Promise<listTripExpensesResponse> {
const tripIdBatches = input.query.trip_ids.reduce<string[][]>(
const tripIdBatches = input.query.tripIds.reduce<string[][]>(
(batches, tripId) => {
const currentBatchIndex = batches.length - 1;

Expand All @@ -152,7 +152,7 @@ export class AbaxClient {

for (const batch of tripIdBatches) {
const response = await this.list150TripExpenses({
query: { trip_ids: batch },
query: { tripIds: batch },
});

expenses.push(response.items);
Expand All @@ -167,7 +167,7 @@ export class AbaxClient {
async getOdometerValuesOfTrips(
input: GetOdometerValuesOfTripsInput,
): Promise<GetOdometerValuesOfTripsResponse> {
const tripIdBatches = input.query.trip_ids.reduce<string[][]>(
const tripIdBatches = input.query.tripIds.reduce<string[][]>(
(batches, tripId) => {
const currentBatchIndex = batches.length - 1;

Expand All @@ -188,7 +188,7 @@ export class AbaxClient {

for (const batch of tripIdBatches) {
const response = await this.getOdometerValuesOf150Trips({
query: { trip_ids: batch },
query: { tripIds: batch },
});

odometerValues.push(response.items);
Expand Down Expand Up @@ -219,22 +219,24 @@ export class AbaxClient {
.args<{ input: ListEquipmentInput }>()
.method('get')
.path('/v2/equipment/')
.query(({ input: { page, page_size, unit_types } }) => {
const queryParams = new URLSearchParams();
.query(
({ input: { page, pageSize: pageSize, unitTypes: unitTypes } }) => {
const queryParams = new URLSearchParams();

if (page) {
queryParams.append('page', String(page));
}
if (page_size) {
queryParams.append('page_size', String(page_size));
}
if (page) {
queryParams.append('page', String(page));
}
if (pageSize) {
queryParams.append('page_size', String(pageSize));
}

if (unit_types) {
queryParams.append('unit_types', String(unit_types));
}
if (unitTypes) {
queryParams.append('unit_types', String(unitTypes));
}

return queryParams;
})
return queryParams;
},
)
.parseJson(withZod(listEquipmentResponse))
.build();

Expand All @@ -249,24 +251,24 @@ export class AbaxClient {
.args<{ input: ListEquipmentLogsInput }>()
.method('get')
.path('/v2/equipment/usage-log')
.query(({ input: { page, page_size, date_from, date_to } }) => {
.query(({ input: { page, pageSize, dateFrom, dateTo } }) => {
const queryParams = new URLSearchParams();

queryParams.append(
'date_from',
format(date_from, "yyyy-MM-dd'T'HH:mm:ssxxx"),
format(dateFrom, "yyyy-MM-dd'T'HH:mm:ssxxx"),
);

queryParams.append(
'date_to',
format(date_to, "yyyy-MM-dd'T'HH:mm:ssxxx"),
format(dateTo, "yyyy-MM-dd'T'HH:mm:ssxxx"),
);

if (page) {
queryParams.append('page', String(page));
}
if (page_size) {
queryParams.append('page_size', String(page_size));
if (pageSize) {
queryParams.append('page_size', String(pageSize));
}

return queryParams;
Expand All @@ -290,11 +292,11 @@ export class AbaxClient {
private list150TripExpenses(
input: ListTripExpensesInput,
): Promise<listTripExpensesResponse> {
if (input.query.trip_ids.length === 0) {
if (input.query.tripIds.length === 0) {
return Promise.resolve({ items: [] });
}

if (input.query.trip_ids.length > 150) {
if (input.query.tripIds.length > 150) {
return this.listTripExpenses(input);
}

Expand All @@ -304,7 +306,7 @@ export class AbaxClient {
.path('v1/trips/expense')
.query(({ input }) => {
const params = new URLSearchParams();
input.query.trip_ids.forEach(id => params.append('trip_ids', id));
input.query.tripIds.forEach(id => params.append('trip_ids', id));
return params;
})
.parseJson(withZod(listTripExpensesSchema))
Expand All @@ -319,7 +321,7 @@ export class AbaxClient {
page: number,
): Promise<Trip[]> {
const response = await this.listTripsPage({
query: { ...input.query, page_size: 1500, page },
query: { ...input.query, pageSize: 1500, page },
});

if (response.items.length >= 1500) {
Expand All @@ -342,10 +344,10 @@ export class AbaxClient {
makeQuery({
query: {
page: input.query.page,
page_size: input.query.page_size,
date_from: format(input.query.date_from, 'yyyy-MM-dd'),
date_to: format(input.query.date_to, 'yyyy-MM-dd'),
vehicle_id: input.query.vehicle_id,
page_size: input.query.pageSize,
date_from: format(input.query.dateFrom, 'yyyy-MM-dd'),
date_to: format(input.query.dateTo, 'yyyy-MM-dd'),
vehicle_id: input.query.vehicleId,
},
}),
)
Expand All @@ -358,10 +360,10 @@ export class AbaxClient {
private async getOdometerValuesOf150Trips(
input: GetOdometerValuesOfTripsInput,
): Promise<GetOdometerValuesOfTripsResponse> {
if (input.query.trip_ids.length === 0) {
if (input.query.tripIds.length === 0) {
return Promise.resolve({ items: [] });
}
if (input.query.trip_ids.length > 150) {
if (input.query.tripIds.length > 150) {
return this.getOdometerValuesOfTrips(input);
}

Expand All @@ -372,11 +374,11 @@ export class AbaxClient {
.query(
({
input: {
query: { trip_ids },
query: { tripIds },
},
}) => {
const params = new URLSearchParams();
trip_ids.forEach(trip => params.append('trip_ids', trip));
tripIds.forEach(trip => params.append('trip_ids', trip));
return params;
},
)
Expand Down
16 changes: 8 additions & 8 deletions src/authentication/__tests__/decode-abax-profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ describe('decode-abax.profile.ts', () => {
exp: 123,
aud: 'hello',
amr: ['hello'],
at_hash: '123abc',
sid: '123EF',
sub: '123',
auth_time: 123,
idp: 'local',
'http://schemas.abax.no/identity/claims/username': 'hello@example.com',
role: 'Administrator',
name: 'Name Namey',
locale: 'no',
'http://schemas.abax.no/identity/claims/organizationid': '123',
'http://schemas.abax.no/identity/claims/countrycode': 'NO',
email: 'hello@example.com',
email_verified: true,
phone_number: '+4799999999',
'http://schemas.abax.no/identity/claims/securitystamp': '123abc',
username: 'hello@example.com',
organizationId: '123',
countryCode: 'NO',
securityStamp: '123abc',
atHash: '123abc',
authTime: 123,
emailVerified: true,
phoneNumber: '+4799999999',
});
});
it('should fail to decode a bad token', () => {
Expand Down
70 changes: 47 additions & 23 deletions src/authentication/decode-abax-profile.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
import { createDecoder } from 'fast-jwt';
import { z } from 'zod';

export const abaxIdTokenPayload = z.object({
iss: z.string(),
nbf: z.number(),
iat: z.number(),
exp: z.number(),
aud: z.string(),
amr: z.array(z.string()),
at_hash: z.string(),
sid: z.string(),
sub: z.string(),
auth_time: z.number(),
idp: z.string(),
'http://schemas.abax.no/identity/claims/username': z.string(),
role: z.string(),
name: z.string(),
locale: z.string(),
'http://schemas.abax.no/identity/claims/organizationid': z.string(),
'http://schemas.abax.no/identity/claims/countrycode': z.string(),
email: z.string(),
email_verified: z.boolean(),
phone_number: z.string().optional(),
'http://schemas.abax.no/identity/claims/securitystamp': z.string(),
});
export const abaxIdTokenPayload = z
.object({
iss: z.string(),
nbf: z.number(),
iat: z.number(),
exp: z.number(),
aud: z.string(),
amr: z.array(z.string()),
at_hash: z.string(),
sid: z.string(),
sub: z.string(),
auth_time: z.number(),
idp: z.string(),
'http://schemas.abax.no/identity/claims/username': z.string(),
role: z.string(),
name: z.string(),
locale: z.string(),
'http://schemas.abax.no/identity/claims/organizationid': z.string(),
'http://schemas.abax.no/identity/claims/countrycode': z.string(),
email: z.string(),
email_verified: z.boolean(),
phone_number: z.string().optional(),
'http://schemas.abax.no/identity/claims/securitystamp': z.string(),
})
.transform(
({
at_hash,
auth_time,
email_verified,
phone_number,
'http://schemas.abax.no/identity/claims/username': username,
'http://schemas.abax.no/identity/claims/organizationid': organizationId,
'http://schemas.abax.no/identity/claims/countrycode': countryCode,
'http://schemas.abax.no/identity/claims/securitystamp': securityStamp,
...data
}) => ({
...data,
username,
organizationId,
countryCode,
securityStamp,
atHash: at_hash,
authTime: auth_time,
emailVerified: email_verified,
phoneNumber: phone_number,
}),
);

export type AbaxIdTokenPayload = z.infer<typeof abaxIdTokenPayload>;

Expand Down
20 changes: 11 additions & 9 deletions src/calls/__tests__/__snapshots__/get-equipment.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,42 @@
exports[`get-equipment > should return equipment 1`] = `
{
"alias": "Concrete Pump Yellow",
"asset_id": "947a0543bb4446dc98e17069f659be7c",
"assetId": "947a0543bb4446dc98e17069f659be7c",
"id": "90c5b884d50b46afa6eb3aa14792f782",
"initial_operating_hours": {
"initialOperatingHours": {
"hours": 5,
"unit_driven": true,
"unitDriven": true,
},
"location": {
"in_movement": false,
"accuracyRadius": undefined,
"inMovement": false,
"latitude": 67.879802,
"longitude": 12.977594,
"signal_source": "Gps",
"signalSource": "Gps",
"timestamp": "2019-09-27T08:59:41.821+00:00",
},
"model": {
"name": "Reed Concrete Pump C50-HP",
},
"notes": "To be used only in Oslo",
"operating_hours": {
"operatingHours": {
"hours": 100,
"unit_driven": true,
"unitDriven": true,
},
"organization": {
"id": "9953da6a1ce7404591680b20fc67a1b1",
"name": "ABAX",
},
"serial_number": "UHR000123",
"registeredAt": undefined,
"serialNumber": "UHR000123",
"temperature": {
"timestamp": "2019-09-27T08:59:41.821+00:00",
"value": 20,
},
"unit": {
"health": "Healthy",
"id": "3ba46063c2064276becc484d3dc432fc",
"serial_number": "PCX000123",
"serialNumber": "PCX000123",
"status": "Active",
"type": "Abax5",
},
Expand Down
Loading
Loading