Skip to content

Commit

Permalink
Omit needless code (headers are set in hook)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwijos committed Jan 11, 2024
1 parent b03279b commit 7bd7c96
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 92 deletions.
30 changes: 8 additions & 22 deletions src/routes/(protected)/admin/bikes/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { PUBLIC_REST_API_URL } from '$env/static/public';

import { fail } from '@sveltejs/kit';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`, {
method: 'GET'
}).then((r) => r.json())
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`).then((r) => r.json())
};
};

const deactivate: Action = async ({ request, cookies }) => {
const deactivate: Action = async ({ request, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand All @@ -23,11 +21,7 @@ const deactivate: Action = async ({ request, cookies }) => {

try {
const response = await fetch(`${PUBLIC_REST_API_URL}/admin/bikes/${bikeId}/deactivate`, {
method: 'PUT',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
method: 'PUT'
});

if (!response.ok) {
Expand All @@ -48,7 +42,7 @@ const deactivate: Action = async ({ request, cookies }) => {
return { success: true };
};

const activate: Action = async ({ request, cookies }) => {
const activate: Action = async ({ request, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand All @@ -59,11 +53,7 @@ const activate: Action = async ({ request, cookies }) => {

try {
const response = await fetch(`${PUBLIC_REST_API_URL}/admin/bikes/${bikeId}/activate`, {
method: 'PUT',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
method: 'PUT'
});

if (!response.ok) {
Expand All @@ -84,7 +74,7 @@ const activate: Action = async ({ request, cookies }) => {
return { success: true };
};

const changeStatus: Action = async ({ request, cookies }) => {
const changeStatus: Action = async ({ request, fetch }) => {
const data = await request.formData();

const bikeId = data.get('id');
Expand All @@ -100,11 +90,7 @@ const changeStatus: Action = async ({ request, cookies }) => {
const response = await fetch(
`${PUBLIC_REST_API_URL}/admin/bikes/${bikeId}/status/${statusId}`,
{
method: 'PUT',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
method: 'PUT'
}
);

Expand Down
6 changes: 2 additions & 4 deletions src/routes/(protected)/admin/bikes/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ params }) => {
export const load: PageServerLoad = async ({ params, fetch }) => {
return {
bike: await fetch(`${PUBLIC_REST_API_URL}/bikes/${params.id}`, {
method: 'GET'
}).then((r) => r.json())
bike: await fetch(`${PUBLIC_REST_API_URL}/bikes/${params.id}`).then((r) => r.json())
};
};
6 changes: 2 additions & 4 deletions src/routes/(protected)/admin/map/live/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`, {
method: 'GET'
}).then((r) => r.json())
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`).then((r) => r.json())
};
};
6 changes: 2 additions & 4 deletions src/routes/(protected)/admin/map/test/animate/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`, {
method: 'GET'
}).then((r) => r.json())
bikes: await fetch(`${PUBLIC_REST_API_URL}/bikes`).then((r) => r.json())
};
};
10 changes: 3 additions & 7 deletions src/routes/(protected)/admin/map/zones/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async () => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
zones: await fetch(`${PUBLIC_REST_API_URL}/zones`, {
method: 'GET'
}).then((r) => r.json()),
cities: await fetch(`${PUBLIC_REST_API_URL}/cities`, {
method: 'GET'
}).then((r) => r.json())
zones: await fetch(`${PUBLIC_REST_API_URL}/zones`).then((r) => r.json()),
cities: await fetch(`${PUBLIC_REST_API_URL}/cities`).then((r) => r.json())
};
};
10 changes: 2 additions & 8 deletions src/routes/(protected)/admin/users/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch }) => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
users: await fetch(`${PUBLIC_REST_API_URL}/admin/users`, {
method: 'GET',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
}).then((r) => r.json())
users: await fetch(`${PUBLIC_REST_API_URL}/admin/users`).then((r) => r.json())
};
};
19 changes: 2 additions & 17 deletions src/routes/(protected)/admin/users/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch, params }) => {
export const load: PageServerLoad = async ({ fetch, params }) => {
const { id } = params;
const accessToken = cookies.get('access_token');

return {
user: await fetch(`${PUBLIC_REST_API_URL}/admin/users/${params.id}`, {
method: 'GET',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': accessToken
}
}).then((r) => r.json()),
user: await fetch(`${PUBLIC_REST_API_URL}/admin/users/${params.id}`).then((r) => r.json()),
trips: await fetch(`${PUBLIC_REST_API_URL}/admin/trips/limit/5/offset/0`, {
method: 'POST',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': accessToken
},
body: JSON.stringify({ user_id: id })
}).then((r) => r.json()),
transactions: await fetch(`${PUBLIC_REST_API_URL}/admin/transactions/limit/5/offset/0`, {
method: 'POST',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': accessToken
},
body: JSON.stringify({ user_id: id })
}).then((r) => r.json())
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch, params }) => {
export const load: PageServerLoad = async ({ fetch, params }) => {
return {
transactions: await fetch(`${PUBLIC_REST_API_URL}/admin/transactions`, {
method: 'POST',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
},
body: JSON.stringify({ user_id: params.id })
}).then((r) => r.json())
};
Expand Down
7 changes: 2 additions & 5 deletions src/routes/(protected)/admin/users/[id]/trips/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch, params }) => {
export const load: PageServerLoad = async ({ fetch, params }) => {
return {
trips: await fetch(`${PUBLIC_REST_API_URL}/admin/trips`, {
method: 'POST',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
},

body: JSON.stringify({ user_id: params.id })
}).then((r) => r.json())
};
Expand Down
10 changes: 2 additions & 8 deletions src/routes/(protected)/admin/users/transactions/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch }) => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
transactions: await fetch(`${PUBLIC_REST_API_URL}/admin/transactions/all`, {
method: 'GET',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
}).then((r) => r.json())
transactions: await fetch(`${PUBLIC_REST_API_URL}/admin/transactions/all`).then((r) => r.json())
};
};
10 changes: 2 additions & 8 deletions src/routes/(protected)/admin/users/trips/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import type { PageServerLoad } from './$types';
import { PUBLIC_REST_API_URL } from '$env/static/public';

export const load: PageServerLoad = async ({ cookies, fetch }) => {
export const load: PageServerLoad = async ({ fetch }) => {
return {
trips: await fetch(`${PUBLIC_REST_API_URL}/admin/trips/all`, {
method: 'GET',
// @ts-expect-error - We are aware that 'x-access-token' is not typed
headers: {
'x-access-token': cookies.get('access_token')
}
}).then((r) => r.json())
trips: await fetch(`${PUBLIC_REST_API_URL}/admin/trips/all`).then((r) => r.json())
};
};

0 comments on commit 7bd7c96

Please sign in to comment.