Skip to content

Commit

Permalink
Remove user mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
szuperaz committed May 15, 2024
1 parent a04221a commit 56013dc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 166 deletions.
17 changes: 7 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ $ yarn generate:open-api
$ yarn generate:open-api:dev
```

If you want to update only chat or video you need to define the `PRODUCT` env variable like this:

```shell
$ PRODUCT=video yarn generate:open-api
$ PRODUCT=chat yarn generate:open-api:dev
```

### Fix issues in chat code
### Fix issues

If you have updated the generated chat code you'll have to fix the following issues manually in the generated code:

- Add `/** @ts-expect-error */ ` (make sure to use this exact comment format otherwise they will be missing from `d.ts` files) for imports for `ImageSizeRequest`, `OnlyUserIDRequest` in the `gen/chat/FilesApi.ts` and `gen/chat/MessagesApi.ts` files
- Add `/** @ts-expect-error */ ` (make sure to use this exact comment format otherwise they will be missing from `d.ts` files) for duplicate exports in `gen/chat/index.ts`
In the `src/gen/apis/ProductchatApi.ts` file add a `/** @ts-expect-error */` (make sure to use this exact format) annotation:

```ts
/** @ts-expect-error */
OnlyUserID;
```

### Validate that the generated code works

Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './src/gen/chat/models';
export * from './src/gen/video/models';
export * from './src/gen/models';
export * from './src/gen/apis';
export * from './src/StreamClient';
export * from './src/StreamVideoClient';
export * from './src/StreamCall';
Expand Down
1 change: 0 additions & 1 deletion src/StreamCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
DeleteRecordingRequest,
DeleteTranscriptionRequest,
GetCallRequest,
ProductcommonApi,
ProductvideoApi,
} from './gen/apis';

Expand Down
167 changes: 14 additions & 153 deletions src/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
ExportUserRequest,
ExportUsersRequest,
FlagRequest,
FullUserResponse,
GetPermissionRequest,
GetTaskRequest,
ListDevicesRequest,
Expand All @@ -36,9 +35,6 @@ import {
UpdateUserPartialRequest,
UpdateUsersRequest,
UserCustomEventRequest,
UserObject,
UserRequest,
UserResponse,
} from './gen';

Check failure on line 38 in src/StreamClient.ts

View workflow job for this annotation

GitHub Actions / lint

'./gen' imported multiple times
import {
Configuration,
Expand Down Expand Up @@ -204,30 +200,21 @@ export class StreamClient {
return this.commonApi.checkPush({ checkPushRequest });
};

createGuest = async (createGuestRequest: CreateGuestRequest) => {
const response = await this.commonApi.createGuest({ createGuestRequest });

response.user = this.mapCustomDataAfterReceive(response.user);

return response;
createGuest = (createGuestRequest: CreateGuestRequest) => {
return this.commonApi.createGuest({ createGuestRequest });
};

banUser = (banRequest: BanRequest) => {
banRequest.user = this.mapCustomDataBeforeSend(banRequest.user);
banRequest.banned_by = this.mapCustomDataBeforeSend(banRequest.banned_by);
return this.commonApi.ban({ banRequest });
};

deactivateUser = async (
deactivateUserRequest: DeactivateUserRequest & { user_id: string },
) => {
const response = await this.commonApi.deactivateUser({
return this.commonApi.deactivateUser({

Check failure on line 214 in src/StreamClient.ts

View workflow job for this annotation

GitHub Actions / lint

Returning an awaited promise is required in this context
deactivateUserRequest,
userId: deactivateUserRequest.user_id,
});
response.user = this.mapCustomDataAfterReceive(response.user);

return response;
};

deactivateUsers = (deactivateUsersRequest: DeactivateUsersRequest) => {
Expand All @@ -246,45 +233,25 @@ export class StreamClient {
return this.commonApi.exportUsers({ exportUsersRequest });
};

flag = async (flagRequest: FlagRequest) => {
flagRequest.user = this.mapCustomDataBeforeSend(flagRequest.user);
const response = await this.commonApi.flag({ flagRequest });
if (response.flag?.user) {
response.flag.user = this.mapCustomDataAfterReceive(response.flag?.user);
}

return response;
flag = (flagRequest: FlagRequest) => {
return this.commonApi.flag({ flagRequest });
};

queryBannedUsers = async (payload: QueryBannedUsersRequest) => {
payload.user = this.mapCustomDataBeforeSend(payload.user);
const response = await this.chatApi.queryBannedUsers({ payload });
response.bans.forEach((b) => {
b.banned_by = this.mapCustomDataAfterReceive(b.banned_by);
b.user = this.mapCustomDataAfterReceive(b.user);
});

return response;
queryBannedUsers = (payload: QueryBannedUsersRequest) => {
return this.chatApi.queryBannedUsers({ payload });
};

queryUsers = async (payload: QueryUsersPayload) => {
payload.user = this.mapCustomDataBeforeSend(payload.user);
const response = await this.commonApi.queryUsers({ payload });
response.users = response.users.map((u) =>
this.mapCustomDataAfterReceive(u),
);

return response;
queryUsers = (payload: QueryUsersPayload) => {
return this.commonApi.queryUsers({ payload });
};

reactivateUser = async (
reactivateUser = (
reactivateUserRequest: ReactivateUserRequest & { user_id: string },
) => {
const response = await this.commonApi.reactivateUser({
return this.commonApi.reactivateUser({
reactivateUserRequest,
userId: reactivateUserRequest.user_id,
});
response.user = this.mapCustomDataAfterReceive(response.user);
};

reactivateUsers = (reactivateUsersRequest: ReactivateUsersRequest) => {
Expand All @@ -300,58 +267,22 @@ export class StreamClient {
};

upsertUsers = async (updateUsersRequest: UpdateUsersRequest) => {
Object.keys(updateUsersRequest.users).forEach((key) => {
updateUsersRequest.users[key] = this.mapCustomDataBeforeSend(
updateUsersRequest.users[key],
);
});
const response = await this.commonApi.updateUsers({ updateUsersRequest });
Object.keys(response.users).forEach((key) => {
response.users[key] = this.mapCustomDataAfterReceive(
response.users[key],
)!;
});

return response;
return this.commonApi.updateUsers({ updateUsersRequest });

Check failure on line 270 in src/StreamClient.ts

View workflow job for this annotation

GitHub Actions / lint

Returning an awaited promise is required in this context
};

updateUsersPartial = async (request: {
users: UpdateUserPartialRequest[];
}) => {
const response = await this.commonApi.updateUsersPartial({
return this.commonApi.updateUsersPartial({

Check failure on line 276 in src/StreamClient.ts

View workflow job for this annotation

GitHub Actions / lint

Returning an awaited promise is required in this context
updateUsersPartialRequest: request,
});
Object.keys(response.users).forEach((key) => {
response.users[key] = this.mapCustomDataAfterReceive(
response.users[key],
)!;
});

return response;
};

muteUser = async (muteUserRequest: MuteUserRequest) => {
muteUserRequest.user = this.mapCustomDataBeforeSend(muteUserRequest.user);
const response = await this.commonApi.muteUser({ muteUserRequest });
if (response.mute?.user) {
response.mute.user = this.mapCustomDataAfterReceive(response.mute?.user);
}
if (response.mutes) {
response.mutes = response.mutes.map((m) => {
return {
...m,
user: this.mapCustomDataAfterReceive(m.user),
};
});
}

return response;
return this.commonApi.muteUser({ muteUserRequest });

Check failure on line 282 in src/StreamClient.ts

View workflow job for this annotation

GitHub Actions / lint

Returning an awaited promise is required in this context
};

unmuteUser = (unmuteUserRequest: UnmuteUserRequest) => {
unmuteUserRequest.user = this.mapCustomDataBeforeSend(
unmuteUserRequest.user,
);
return this.commonApi.unmuteUser({ unmuteUserRequest });
};

Expand Down Expand Up @@ -502,74 +433,4 @@ export class StreamClient {
},
});
};

private readonly reservedKeywords = [
'ban_expires',
'banned',
'id',
'invisible',
'language',
'push_notifications',
'revoke_tokens_issued_before',
'role',
'teams',
'created_at',
'deactivated_at',
'deleted_at',
'last_active',
'online',
'updated_at',
'shadow_banned',
'name',
'image',
];

private readonly mapCustomDataBeforeSend = (
user: UserObject | UserRequest | UserResponse | undefined,
) => {
// if (!user) {
// return undefined;
// }
// if (!user.custom) {
// user.custom = {};
// }

// user.custom.name = user.name;
// user.custom.image = user.image;

// delete user.name;
// delete user.image;

return user;
};

private mapCustomDataAfterReceive<
T = UserObject | UserResponse | undefined | FullUserResponse,
>(user: T): T {
if (!user) {
return user;
}
// /** @ts-expect-error */
// const result: UserObject | UserResponse | FullUserResponse = {};
// Object.keys((user as any).Custom).forEach((key) => {
// if (!this.reservedKeywords.includes(key)) {
// if (!result.custom) {
// result.custom = {};
// }
// result.custom[key] = (user as any).Custom[key];
// } else {
// /** @ts-expect-error */
// result[key] = (user as any).Custom[key];
// }
// });

// Object.keys(user).forEach((key) => {
// if (key !== 'Custom') {
// /** @ts-expect-error */
// result[key] = (user as any)[key];
// }
// });

return user as T;
}
}
1 change: 1 addition & 0 deletions src/gen/apis/ProductchatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import type {
MessageResponse,
MuteChannelRequest,
MuteChannelResponse,
/** @ts-expect-error */
OnlyUserID,
PollOptionResponse,
PollResponse,
Expand Down

0 comments on commit 56013dc

Please sign in to comment.